将变量传递给异步函数

时间:2018-01-04 13:25:36

标签: node.js async.js

我从另一个问题中找到了这个,我确实理解并设法让它发挥作用。

但我的问题是,如何将变量传入第一个函数?

var myTestSubject = "hello"; // i want to pass in this variable into the subject

async.waterfall([
    function(callback){
        //i want to use the variable myTestSubject here
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});

我试图把它放到像这样的代码中,但似乎它不起作用。知道怎么做吗?

var myTestSubject = "hello";
async.waterfall([
    function(callback, myTestSubject ){
        console.log(myTestSubject) // this is undefined
        callback(null, 'one', 'two');
    }], function (err, result) {
       // result now equals 'done'    
    });

2 个答案:

答案 0 :(得分:0)

瀑布的第一次回调不带任何参数。在你的情况下,args的顺序也是不正确的。以下应该做的工作

var myTestSubject = "hello";
async.waterfall([
    function(callback){
      callback(null, myTestSubject)
    },
    function(myTestSubject, callback){
        console.log(myTestSubject) // this will work
        callback(null, 'done');
    }], function (err, result) {
       // result now equals 'done'    
    });

答案 1 :(得分:0)

无需将myTestSubject作为参数传递,因为它位于内部范围内。

这是一个有助于您理解的评论示例。

function test() {
    //start of functions test scope

    var myTestSubject = "hello"; // i want to pass in this variable into the subject

    var two = 'not two';
    console.log(two)
    async.waterfall([
        function(callback) {
            // this is the scope of function(callback) 
            // but this scope is inside of function test() scope 
            // so everything that is above here you can use
            console.log(myTestSubject)

            //one is in this scope so its not available in the next function
            // you will have to pass it as argument
            var one = 'one';
            two = 'two'
            callback(null, one, 'three');
        },
        function(arg1, arg2, callback) {
            // every function has its own scope and you can also access the outer scope
            // but you cant access the sibling scope
            // you will have to pass it as argument

            // arg1 now equals 'one' and arg2 now equals 'three'
            console.log(two)
            callback(null, 'three');
        },
        function(two, callback) {

            // in this scope you have a two variable so two is 'three'
            console.log(two)
            callback(null, 'done');
        },
        function(arg1, callback) {

            // in this scope two is 'two'
            console.log(two)

            //but if you do 
            var two = 'three';
            // now you have 'three' into two
            callback(null, 'done');
        },
        function(arg1, callback) {

            // in this scope you dont  two is 'two'
            console.log(two)
            callback(null, 'done');


        }
    ], function(err, result) {
        // result now equals 'done'    
    });

    //end of function test scope
}