如何重复使用异步回调函数?

4

我在两个函数中使用了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 Kling
@FelixKling,你能简要解释一下吗?你的意思是说 onefunction(testuser) - Prashant Tapase
1
好的,我想你也必须传递callback。由于你需要将函数传递给async.parallel(看起来是这样),所以应该是callback => onefunction(testuser, callback)等。 - Felix Kling
@PrashantTapase,secondfunction的定义在哪里?我只看到声明了twofunction - Amol M Kulkarni
1
@Felix King,你在你的回答中使用了ES6箭头函数,对吗? - sudeep_dk
显示剩余5条评论
2个回答

0

正如@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......}

-2

这是如何在onefunctionsecondfunction等中访问testuser值的方法。
代码:

var async = require('async');

function one(){
    var testuser = 1;
    //You can bind a context here; remember to pass the context i.e. null for this, otherwise it won't work as expected.
    async.parallel({one_func:onefunction.bind(null,testuser), two_func:twofunction.bind(null,testuser)}, function(err, result){
        console.log("In one()");
        console.log("err",err);
        console.log("result",result); //result array with all task's results e.g. one_func, two_func
        console.log("testuser",testuser);
 })
}

function onefunction(testuser,cb){
    console.log('onefunction');
    return cb(null,{"testuser":testuser});
}

function twofunction(testuser,cb){
    console.log('twofunction');
    return cb(null,{"testuser":testuser});
}

one();

更新:关于在标题中关于重用回调的另一个问题的答案。
你可以将命名函数作为回调函数重用吗?
示例代码:

function one(){
    var testuser = 1;
    async.parallel({one_func:onefunction.bind(null,testuser), two_func:twofunction.bind(null,testuser)},calback)
}

function calback (err, result){
        console.log("calback"); //You can reuse this named function as callbacks
        console.log("err",err);
        console.log("result",result);
 }

回调函数 => onefunction(testuser, callback) 这段代码对我有效.. 感谢 Amol 的尝试。 - Prashant Tapase
@PrashantTapase:你能在这里发布你自己的答案(并接受它),以便也有助于其他人。 谢谢 :) - Amol M Kulkarni
@AmolMKulkarni 检查 - Prashant Tapase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接