node异步编程

串行执行

(function next(i, len, callback) {
    if(i < len) {
        async(arr[i], function(value) {
            arr[i] = value;
            next(i + 1, len, callback);
        });
    } else {
        callback();
    }
}(0, arr.length, function() {
    // All array items have processed.
}));

并行执行

(function(i, len, count, callback) {
    for(;i < len; ++i) {
        (function(i) {
            async(arr[i], function(value) {
                arr[i] = value;
                if(++count === len) {
                    callback();
                }
            });
        }(i));
    }
}(0, arr.length, 0, function() {
    //All array items have processed.
}));

https://www.cnblogs.com/TwinklingZ/p/5995689.html

posted @ 2018-04-22 11:52  SharkChilli  阅读(104)  评论(0编辑  收藏  举报