Talking about async asynchronous programming in node. js, node. jsasync
1. What is asynchronous programming?
Asynchronous programming refers to the failure to synchronously obtain execution results due to factors such as asynchronous I/O,
The coding style of the next operation in the callback function, such as the setTimeout Function and ajax request.
Example:
for (var i = 1; i <= 3; i++) {setTimeout(function(){console.log(i);}, 0);};
Most people think that the output is 123 or 333. In fact, it will output 444
This is what we want to talk about asynchronous programming.
Definition of advanced functions
Why do we talk about advanced functions here, because advanced functions are the basis of asynchronous programming.
So what are advanced functions?
In fact, advanced functions use functions as parameters or return values.
Example:
function test(v){return function(){return v;}}
The preceding example uses a function as a return value.
2. Process Control
Functions:
Series
Waterfall
Parallel
ParallelLimit
...
Series Function serial execution
It is executed in sequence.
async.series({ one: function(callback){ callback(null, 1); }, two: function(callback){ callback(null, 2); }},function(err, results) { console.log(results);});
Output: {one: 1, two: 2}
The first parameter of the series function can be an array or a JSON object,
Different parameter types affect the format of returned data.
Waterfall function waterfall stream
Waterfall and series functions have many similarities and are executed in order.
The difference is that the values produced by each function in waterfall will be passed to the next function, while series does not. The example is as follows:
async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ // arg1 now equals 'one' and arg2 now equals 'two' callback(null, 'three'); }, function(arg1, callback){ // arg1 now equals 'three' callback(null, 'done'); }], function (err, result) { // result now equals 'done' console.log(result);});
In addition, it should be noted that the tasks parameter of waterfall can only be of the array type.
When a function error occurs in the middle, the err is directly passed to the final callback. The result is discarded and the subsequent function is not executed.
Parallel (tasks, [callback])
Parallel functions execute multiple functions in parallel. Each function is executed immediately without waiting for other functions to be executed first.
The data in the array passed to the final callback is declared in the tasks sequence, rather than the execution completion sequence. The example is as follows:
async.parallel([ function(callback){ callback(null, 'one'); }, function(callback){ callback(null, 'two'); }],function(err, results){});
The tasks parameter can be an array or json object, which is the same as the series function,
If the tasks parameter type is different, the returned results format is different.
ParallelLimit (tasks, limit, [callback])
The parallelLimit function is similar to parallel, but it has an additional parameter limit.
The limit parameter limits the number of concurrent tasks at the same time, rather than unlimited concurrency. For example:
async.parallelLimit([ function(callback){ callback(null, 'one'); }, function(callback){ callback(null, 'two'); }],
2,
function(err, results){ console.log(results);});