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

后端 未结 4 1260
礼貌的吻别
礼貌的吻别 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) })
    

提交回复
热议问题