【问题标题】:How to re-use async callback function?如何重用异步回调函数?
【发布时间】:2016-09-07 18:56:14
【问题描述】:

我在两个具有相同回调函数的函数中使用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...我该怎么做?

【问题讨论】:

  • 使其成为函数的参数并将其传递给它们。
  • @FelixKling 你能简单解释一下吗?你的意思是说onefunction(testuser)
  • 好吧,我假设您还必须通过callback。而且由于您必须将函数传递给async.parallel(看起来如此),它应该是callback => onefunction(testuser, callback)
  • @PrashantTapase secondfunction 的定义在哪里?我只能看到 twofunction 声明。
  • @Felix King 在您的回答中您正在使用 ES6 箭头功能对.. ?

标签: node.js express asynchronous


【解决方案1】:

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

    【讨论】:

    • callback => onefunction(testuser, callback) 这个代码对我有用.. 感谢 amol 尝试
    • @PrashantTapase:您能否在此处发布您自己的答案(并接受它),以便对其他人也有所帮助。谢谢你:)
    • @AmolMKulkarni chk
    猜你喜欢
    • 2020-06-04
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 2020-03-09
    • 2018-10-03
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多