代码之家  ›  专栏  ›  技术社区  ›  Think Tank

将变量传递到异步函数

  •  0
  • Think Tank  · 技术社区  · 7 年前

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

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

    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 回复  |  直到 7 年前
        1
  •  0
  •   Stamos    7 年前

    没有必要通过考试 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
    }
    
        2
  •  0
  •   Ratan Kumar    7 年前

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