将变量传递给异步函数

Passing a variable into asynchonous function

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

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

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'    
    });

瀑布的第一个回调不带任何参数。在您的情况下,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'    
    });

不需要将 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
}