gpt4 book ai didi

javascript - Node js在循环node js中完成多个函数后执行另一个函数

转载 作者:行者123 更新时间:2023-12-03 02:15:36 24 4
gpt4 key购买 nike

我的代码如下-:

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');
}

借助上述代码,我想在所有功能完成后执行 all_completed 循环。在初始启动时,执行 get_label_info 函数,然后 Controller 继续执行 get_btc 函数。

请帮助我在所有函数完成运行后如何运行 all_completed 函数。

最佳答案

试试这个,

在for循环外部定义test_function,在循环内部声明不是好方法。

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');
}

关于javascript - Node js在循环node js中完成多个函数后执行另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com