gpt4 book ai didi

javascript - node.js 异步系列函数的参数

转载 作者:IT老高 更新时间:2023-10-28 23:14:10 25 4
gpt4 key购买 nike

我需要做如下代码:

function taskFirst(k, v) {
console.log(k, v);
}

function taskSecond(k, v) {
console.log(k, v);
}

function run() {
var g1 = "Something";
var g2 = "Something";
var g3 = "Something";
var g4 = "Something";

async.series(
[
taskFirst(g1, g2),
taskSecond(g3, g4)
],
function(error, result){

}
);
}

传递自定义变量和async.js回调函数的正确方法是什么?

最佳答案

你可以这样做:

function taskFirst(k, v, callback) {
console.log(k, v);

// Do some async operation
if (error) {
callback(error);
} else {
callback(null, result);
}
}

function taskSecond(k, v, callback) {
console.log(k, v);

// Do some async operation
if (error) {
callback(error);
} else {
callback(null, result);
}
}

function run() {
var g1 = "Something";
var g2 = "Something";
var g3 = "Something";
var g4 = "Something";

async.series(
[
// Here we need to call next so that async can execute the next function.
// if an error (first parameter is not null) is passed to next, it will directly go to the final callback
function (next) {
taskFirst(g1, g2, next);
},
// runs this only if taskFirst finished without an error
function (next) {
taskSecond(g3, g4, next);
}
],
function(error, result){

}
);
}

关于javascript - node.js 异步系列函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288014/

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