JavaScript, Node.js: is Array.forEach asynchronous?

后端 未结 10 1446
时光说笑
时光说笑 2020-11-22 10:47

I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously? For example, if I call:

[many          


        
10条回答
  •  盖世英雄少女心
    2020-11-22 11:32

    There is a package on npm for easy asynchronous for each loops.

    var forEachAsync = require('futures').forEachAsync;
    
    // waits for one request to finish before beginning the next 
    forEachAsync(['dogs', 'cats', 'octocats'], function (next, element, index, array) {
      getPics(element, next);
      // then after all of the elements have been handled 
      // the final callback fires to let you know it's all done 
      }).then(function () {
        console.log('All requests have finished');
    });
    

    Also another variation forAllAsync

提交回复
热议问题