Callback after all asynchronous forEach callbacks are completed

后端 未结 13 2052
谎友^
谎友^ 2020-11-22 12:56

As the title suggests. How do I do this?

I want to call whenAllDone() after the forEach-loop has gone through each element and done some asynchronous p

相关标签:
13条回答
  • 2020-11-22 13:32

    With ES2018 you can use async iterators:

    const asyncFunction = a => fetch(a);
    const itemDone = a => console.log(a);
    
    async function example() {
      const arrayOfFetchPromises = [1, 2, 3].map(asyncFunction);
    
      for await (const item of arrayOfFetchPromises) {
        itemDone(item);
      }
    
      console.log('All done');
    }
    
    0 讨论(0)
提交回复
热议问题