async控制流

参考:https://caolan.github.io/async/docs.html#waterfall
1, 一般来说,常用的异步控制流有两种,

waterfall

Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.
按顺序运行函数的tasks数组, 每个函数将结果传递给数组的下一个,如果任何一个函数传递了一个错误,则下一个函数不被执行, 主回调会立即调用这个错误。

Parameters:
Name Type Description
tasks Array
An array of async functions to run. Each function should complete with any number of result values. The result values will be passed as arguments, in order, to the next task.
每个函数都应该包含任意数量的结果值。结果值将按顺序作为参数传递给下一个任务
callback function
An optional callback to run once all the functions have completed. This will be passed the results of the last task’s callback. Invoked with (err, [results]).

async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');//这里第一个参数一定要
    },
    function(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        if(!arg1||!args2)
        {
            callback(new Error("xxx"), code);
            return;
        }
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'
});

// Or, with named functions:
async.waterfall([
    myFirstFunction,
    mySecondFunction,
    myLastFunction,
], function (err, result) {
    // result now equals 'done'
});
function myFirstFunction(callback) {
    callback(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, callback) {
    // arg1 now equals 'one' and arg2 now equals 'two'
    callback(null, 'three');
}
function myLastFunction(arg1, callback) {
    // arg1 now equals 'three'
    callback(null, 'done');
}

2 . parallel

parallel

Run the tasks collection of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error. Once the tasks have completed, the results are passed to the final callback as an array.

Note: parallel is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded.

Hint: Use reflect to continue the execution of other tasks when a task fails.

It is also possible to use an object instead of an array. Each property will be run as a function and the results will be passed to the final callback as an object instead of an array. This can be a more readable way of handling results from async.parallel.

Parameters:
Name Type Description
tasks Array | Iterable | Object
A collection of async functions to run. Each async function can complete with any number of optional result values.

callback function
An optional callback to run once all the functions have completed successfully. This function gets a results array (or object) containing all the result arguments passed to the task callbacks. Invoked with (err, results).

翻译: 运行任务函数,不用等前一个函数完成,如果任何一个函数传递一个错误给回调callback,主回调会立即调用错误。

Note:如果您不使用任何计时器或执行任何I / O,它们实际上将被串行执行。每个任务的任何同步设置部分 将一个接一个地发生.JavaScript仍然是单线程的。

提示:当任务失败时,使用reflect继续执行其他任务。

也可以用一个对象来代替数组,每个属性被当作一个函数运行,最后的results将会被传递一个对象, 这样做可读性会更好。

async.parallel([
    function(callback) {
        setTimeout(function() {
            callback(null, 'one');
        }, 200);
    },
    function(callback) {
        setTimeout(function() {
            callback(null, 'two');
        }, 100);
    }
],
// optional callback
function(err, results) {
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.
});

// an example using an object instead of an array
async.parallel({
    one: function(callback) {
        setTimeout(function() {
            callback(null, 1);
        }, 200);
    },
    two: function(callback) {
        setTimeout(function() {
            callback(null, 2);
        }, 100);
    }
}, function(err, results) {
    // results is now equals to: {one: 1, two: 2}
});

猜你喜欢

转载自blog.csdn.net/xiaxuiau/article/details/81353324
今日推荐