How can I wait for set of asynchronous callback functions?

前端 未结 6 1862
感动是毒
感动是毒 2020-11-22 09:07

I have code that looks something like this in javascript:

forloop {
    //async call, returns an array to its callback
}

After ALL of those

6条回答
  •  深忆病人
    2020-11-22 09:38

    You can emulate it like this:

      countDownLatch = {
         count: 0,
         check: function() {
             this.count--;
             if (this.count == 0) this.calculate();
         },
         calculate: function() {...}
      };
    

    then each async call does this:

    countDownLatch.count++;
    

    while in each asynch call back at the end of the method you add this line:

    countDownLatch.check();
    

    In other words, you emulate a count-down-latch functionality.

提交回复
热议问题