How to execute a Javascript function only after multiple other functions have completed?

后端 未结 7 2037
轻奢々
轻奢々 2020-12-25 15:03

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 infor

7条回答
  •  一向
    一向 (楼主)
    2020-12-25 15:57

    If you don't want to use any helper libraries, than you need to write some helper yourself, there's no simple one line solution for this.

    If you'd like to end with something that looks as readable as it would in synchronous case, try some deferred/promise concept implementation (it's still plain JavaScript), e.g. using deferred package you may end up with something as simple as:

    // Invoke one after another:
    funcA()(funcB)(funcC);
    
    // Invoke funcA and funcB simultaneously and afterwards funcC:
    funcA()(funcB())(funcC);
    
    // If want result of both funcA and funcB to be passed to funcC:
    deferred(funcA(), funcB())(funcC);
    

提交回复
热议问题