Reputation: 336
My code is below-:
function get_btc(address) {
address_transaction(address, user_id, coin_key, deposite_txn_fee, function (callback) {
for (var j = 0; j < callback.response.data.txs.length; j++) {
let user_id = callback.user_id;
//some code//
}
});
}
get_label_info(function (err, data) {
var promise = [];
for (var i = 0; i < data.length; i++) {
let address = data[i].address;
var test_function = function (done) {
deposite_model.get_coin_info(function (err, data1) {
var coin_name = data1[0].coin_code;
const return_functions = get_switch(coin_name);
if (return_functions) {
obj[return_functions](address);
}
done(err, data1);
});
};
promise.push(test_function);
}
sample();
});
function sample() {
console.log('all functions has been completed');
}
By the help of above mentioned code i want to excecute all_completed loop when all functions has been completly done. At the initial start get_label_info function is excuted then controller go on to get_btc function.
Please help me how could i run all_completed functions after all functions completed run.
Upvotes: 0
Views: 137
Reputation: 10071
Try this,
Define test_function outside for loop, It's not the good approach to declare inside the loop.
var test_function = function (address) {
return new Promise((resolve, reject) => {
deposite_model.get_coin_info(function (err, data1) {
if (err)
return reject(err);
var coin_name = data1[0].coin_code;
const return_functions = get_switch(coin_name);
if (return_functions) {
obj[return_functions](address);
}
return resolve(data1);
})
})
}
get_label_info(function (err, data) {
var promises = [];
for (var i = 0; i < data.length; i++) {
promises.push(test_function(address));
}
Promise.all(promises).then((data) => {
sample();
})
});
function sample() {
console.log('all functions has been completed');
}
Upvotes: 1