Whats the smartest / cleanest way to iterate async over arrays (or objs)?

后端 未结 4 1253
礼貌的吻别
礼貌的吻别 2020-12-01 04:45

Thats how I do it:

function processArray(array, index, callback) {
    processItem(array[index], function(){
        if(++index === array.length) {
                  


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 04:56

    Checkout the async library, it's made for control flow (async stuff) and it has a lot of methods for array stuff: each, filter, map. Check the documentation on github. Here's what you probably need:

    each(arr, iterator, callback)

    Applies an iterator function to each item in an array, in parallel. The iterator is called with an item from the list and a callback for when it has finished. If the iterator passes an error to this callback, the main callback for the each function is immediately called with the error.

    eachSeries(arr, iterator, callback)

    The same as each only the iterator is applied to each item in the array in series. The next iterator is only called once the current one has completed processing. This means the iterator functions will complete in order.

提交回复
热议问题