node.js - 如何复用异步回调函数?

标签 node.js express asynchronous

我在两个具有相同回调函数的函数中使用 async.parallel .在第二个函数 (two()) 中添加了第三个函数作为加法函数

function one(){
var testuser = 1;
 async.parallel([onefunction, secondfunction], function(err, result){...})
}

function two(){
var testuser = 2;
async.parallel([onefunction, secondfunction, thirdfunction], function(err, result){...})
}

function onefunction(callback){....code with testuser......}
function twofunction(callback){....code with testuser......}
function thirdfunction(callback){....code with testuser......}

问:如何访问 onefunctionsecondfunctionthirdfunction 中的 testuser 值。

现在我得到undefined err。我也试过作为正常的参数传递逻辑 onefunction(testuser),但它不起作用。

我想在多种情况下使用 onefunctiontwofunction...我该怎么做?

最佳答案

正如@Felix 所建议的,

function one(){
var testuser = 1;
 async.parallel([onefunction, secondfunction], function(err, result){...})
}

    function two(){
    var testuser = 2;
    async.parallel([
       callback => onefunction(testuser, callback), 
       callback => twofunction(testuser, callback), 
       callback => thirdfunction(testuser, callback)], function(err, result){...})
    }

    function onefunction(callback){....code with testuser......}
    function twofunction(callback){....code with testuser......}
    function thirdfunction(callback){....code with testuser......}

关于node.js - 如何复用异步回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37178464/

相关文章:

javascript - AngularJS : Initialize service with asynchronous data

JavaScript:如何确保此函数仅返回完全填充的数组?

javascript - Promise.catch 正在吞噬错误

node.js - Nodejs Redis createClient() 函数

node.js - 异步系列nodejs中的async foreach

node.js - 在 Node express 中再次调用相同的请求

javascript - 如何从异步调用返回响应?

javascript - 在 Node 中合并 PDF

node.js - express-restify-mongoose 错误传播

node.js - 在 pg-promise 中返回