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

后端 未结 4 1237
礼貌的吻别
礼貌的吻别 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:54

    As correctly pointed out, you have to use setTimeout, for example:

    each_async = function(ary, fn) {
        var i = 0;
        -function() {
            fn(ary[i]);
            if (++i < ary.length)
                setTimeout(arguments.callee, 0)
        }()
    }
    
    
    each_async([1,2,3,4], function(p) { console.log(p) })
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 04:59

    As pointed in some answer one can use "async" library. But sometimes you just don't want to introduce new dependency in your code. And below is another way how you can loop and wait for completion of some asynchronous functions.

    var items = ["one", "two", "three"];
    
    // This is your async function, which may perform call to your database or
    // whatever...
    function someAsyncFunc(arg, cb) {
        setTimeout(function () {
            cb(arg.toUpperCase());
        }, 3000);
    }
    
    // cb will be called when each item from arr has been processed and all
    // results are available.
    function eachAsync(arr, func, cb) {
        var doneCounter = 0,
            results = [];
        arr.forEach(function (item) {
            func(item, function (res) {
                doneCounter += 1;
                results.push(res);
                if (doneCounter === arr.length) {
                    cb(results);
                }
            });
        });
    }
    
    eachAsync(items, someAsyncFunc, console.log);
    

    Now, running node iterasync.js will wait for about three seconds and then print [ 'ONE', 'TWO', 'THREE' ]. This is a simple example, but it can be extended to handle many situations.

    0 讨论(0)
  • 2020-12-01 05:01

    The easiest way to handle async iteration of arrays (or any other iterable) is with the await operator (only in async functions) and for of loop.

    (async function() {
     for(let value of [ 0, 1 ]) {
      value += await(Promise.resolve(1))
      console.log(value)
     }
    })()

    You can use a library to convert any functions you may need which accept callback to return promises.

    0 讨论(0)
提交回复
热议问题