简体   繁体   English

JavaScript:如何构造两个以上的回调

[英]JavaScript: How to structure more than two callbacks

I've tried searching for this and cannot find the answer anywhere. 我尝试搜索此文件,但找不到任何答案。

I've been using callbacks like this so far: 到目前为止,我一直在使用这样的回调:

function one(param, callback){
    //do stuff to param1
    callback()
}


function two(param){
    //do more stuff to param1
}

one(myParam, two)

This seems to work fine. 这似乎很好。 However, I want to use three functions. 但是,我想使用三个功能。 This doesn't work: 这不起作用:

function one(param, callback){
    //do stuff to param1
    callback();
}

function two(param, callback){
    //do more stuff to param1
    callback();
}

function three(param){
    //do even more stuff to param1
}

one(myParam, two(three))

I'm guessing this is because of the brackets when passing two as a parameter in one, making 'two' excecute immediately. 我猜这是因为将两个作为参数传递给一个括号时,括号使“ two”立即执行。

How do I structure my code so I can excecute the function in the correct order? 如何构造代码,以便可以正确的顺序执行该函数?

This is because you setting three as a parameter of two: 这是因为您将three设置为2的参数:

 function one(param1, callback){ alert(param1); callback("Second Called", three); } function two(param1, callback){ //do more stuff to param1 alert(param1); callback("Third Called"); } function three(param1){ alert(param1); //do even more stuff to param1 } one("First Called", two); 

You need to make little correction. 您无需进行任何更正。

function one(param1, callback){
   //do stuff to param1
   // assuming param1 contains the processed value
    callback(param1,three);
 }

function two(param1, callback){
  //do more stuff to param1
  callback(param1);
}

function three(param1){
   //do even more stuff to param1
}
// call the function like this
one(myParam,two);

Have a look on the library Async.js. 看一下Async.js库。 Its good for handling asynchronous activity. 它适合处理异步活动。 You can better handle such cases and your code will be much more readable. 您可以更好地处理这种情况,并且代码将更具可读性。

function one(param, callback){
    //param = "apple"
    callback(param);
}

function two(paramFromOne){
    //paramFromOne = "apple"
    three(paramFromOne)
}

function three(paramFromTwoViaOne){
    //paramFromTwoViaOne = "apple"
}

//you are invoking two before it can do anything inside the callback context of one
//one("apple", two(three))

//do this instead
one("apple", two)

If you want to specify callbacks that two uses you have to use a closure 如果要指定two使用的回调,则必须使用闭包

function one(param, callback){ 
    //param = "apple"
    callback(param) //the callback here is the returned function in two
}

function two(callback){ //three
    return function(paramViaOne) {
        //paramViaOne = "apple"
        callback(paramViaOne);
    };
}

function three(paramFromTwoViaOne){
    //paramFromTwoViaOne = "apple"
}


one("apple", two(three)) //two(three) returns an anonymous function that is used by one

You could simply control the sequence of execution using an array and a forEach loop. 您可以使用数组和forEach循环简单地控制执行顺序。

var callbacks = [one, two, three];
callbacks.forEach(function(callback){
    callback(param);
});

Ofcourse the assumption here is that the scope of param is global to all of these callback functions. 当然,这里的假设是,所有这些回调函数的参数范围都是全局的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM