如何执行多个其它功能仅在完成后的JavaScript函数? [英] How to execute a Javascript function only after multiple other functions have completed?

查看:120
本文介绍了如何执行多个其它功能仅在完成后的JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的具体问题是我需要执行一个(潜在的)大量的Javascript功能以prepare像一个批处理文件(每个函数调用增加了一些信息,以相同的批处理文件),然后,毕竟那些呼叫完成之后,执行一最终函数发送批处理文件(例如,将其作为一个HTML响应)。我在找这个普通的Javascript编程模式。

My specific problem is that I need to execute a (potentially) large number of Javascript functions to prepare something like a batch file (each function call adds some information to the same batch file) and then, after all those calls are completed, execute a final function to send the batch file (say, send it as an HTML response). I'm looking for a general Javascript programming pattern for this.

一般化的问题:
鉴于Javascript函数FuncA的行(),funcB()和funcC(),我想弄清楚,这样FuncA的行和funcB已经执行后funcC只后执行命令执行的最佳途径。我知道我可以使用嵌套回调函数是这样的:

Generalize problem: Given the Javascript functions funcA(), funcB(), and funcC(), I would to figure out the best way to order execution so that funcC is only executed after after funcA and funcB have executed. I know that I could use nested callback functions like this:

funcA = function() {
    //Does funcA stuff
    funcB();
}
funcB = function() {
    //Does funcB stuff
    funcC();
}

funcA();

我甚至可以使这种模式多一点一般通过传递回调参数,但是,这种解决方案变得相当冗长。

I could even make this pattern a little more general by passing in callback parameters, however, this solution becomes quite verbose.

我也熟悉JavaScript功能的链接,其中一个解决方案可能是:

I am also familiar with Javascript function chaining where a solution might look like:

myObj = {}
myObj.answer = ""
myObj.funcA = function() {
    //Do some work on this.answer
    return this;
}
myObj.funcB = function() {
    //Do some more work on this.answer
    return this;
}
myObj.funcC = function() {
    //Use the value of this.answer now that funcA and funcB have made their modifications
    return this;
}
myObj.funcA().funcB().funcC();

虽然这个解决方案似乎干净了一点对我来说,当您添加更多措施来计算,函数执行的链长越来越长。

While this solution seems a little cleaner to me, as you add more steps to the computation, the chain of function executions grows longer and longer.

有关我的具体问题,其中FuncA的行,funcB等的执行顺序并不重要。因此,在我上面的解决方案,我在技术上做的比,因为我把所有的功能于一身的串行顺序,需要更多的工作。所有的事情对我来说是funcC(某些功能将结果发送或发射过的请求)后FuncA的行和funcB已全部执行完毕,才会调用。理想情况下,funcC能够以某种方式监听所有的中间函数调用来完成,然后将执行?我希望了解的一般的Javascript模式来解决这样的问题。

For my specific problem, the order in which funcA, funcB, etc. are executed DOES NOT matter. So in my solutions above, I am technically doing more work than is required because I am placing all the functions in a serial ordering. All that matters to me is that funcC (some function for sending the result or firing off a request) is only called after funcA and funcB have ALL completed execution. Ideally, funcC could somehow listen for all the intermediate function calls to complete and THEN would execute? I hoping to learn a general Javascript pattern to solve such a problem.

感谢您的帮助。

另一个想法:
也许一个共享对象传递FuncA的行和funcB,当他们完成执行标记共享对象像sharedThing.funcA =完全或sharedThing.funcB =完成,然后不知何故?有当共享对象达到,所有字段标完整的状态funcC执行。我不知道你能究竟是如何让funcC等待这一点。

Another Idea: Maybe pass a shared object to funcA and funcB and when they complete execution mark the shared object like sharedThing.funcA = "complete" or sharedThing.funcB = "complete" and then somehow? have funcC execute when the shared object reaches a state where all fields are marked complete. I'm not sure how exactly you could make funcC wait for this.

编辑:
我要指出,我使用服务器端JavaScript(Node.js的),我想了解一个模式,它只是使用普通的旧的JavaScript(不使用jQuery或其他库)来解决。当然,这个问题是一般足够,有一个干净的纯JavaScript解决方案?

I should note that I'm using server-side Javascript (Node.js) and I would like to learn a pattern to solve it just using plain old Javascript (without the use of jQuery or other libraries). Surely this problem is general enough that there is a clean pure-Javascript solution?

推荐答案

如果你想保持简单,你可以使用一个基于计数器的回调系统。这里是一个系统,允许时(A,B)。然后(C)语法的草案。 ( / 然后其实只是糖分,但随后又对整个系统可以说是就是。)

If you want to keep it simple, you can use a counter-based callbacks system. Here's a draft of a system that allows when(A, B).then(C) syntax. (when/then is actually just sugar, but then again the whole system arguably is.)

var when = function() {
  var args = arguments;  // the functions to execute first
  return {
    then: function(done) {
      var counter = 0;
      for(var i = 0; i < args.length; i++) {
        // call each function with a function to call on done
        args[i](function() {
          counter++;
          if(counter === args.length) {  // all functions have notified they're done
            done();
          }
        });
      }
    }
  };
};

用法:

when(
  function(done) {
    // do things
    done();
  },
  function(done) {
    // do things
    setTimeout(done, 1000);
  },
  ...
).then(function() {
  // all are done
});

这篇关于如何执行多个其它功能仅在完成后的JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆