Home >Web Front-end >JS Tutorial >A brief discussion on async asynchronous programming in node.js_node.js
1. What is asynchronous programming?
Asynchronous programming refers to when the execution results cannot be obtained synchronously due to asynchronous I/O and other factors,
The coding style of performing the next operation in the callback function, common ones such as setTimeout function, ajax request, etc.
Example:
for (var i = 1; i <= 3; i++) { setTimeout(function(){ console.log(i); }, 0); };
Most people here will think that the output is 123, or 333. Actually it will output 444
Here is what we are talking about asynchronous programming.
Definition of advanced functions
Why are we talking about high-level functions here? Because high-level functions are the basis of asynchronous programming.
So what are advanced functions?
In fact, advanced functions take functions as parameters or as return values.
Example:
function test(v){ return function(){ return v; } }
The above is to use a function as a return value.
2. Process control
The functions are:
series
waterfall
parallel
parallelLimit
…
series function serial execution
Its function is to execute it 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 the returned data.
waterfall function waterfall flow
Waterfall and series functions have many similarities, both are executed in order.
The difference is that the value generated by each function in waterfall will be passed to the next function, while series does not have this function. An 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); });
It should also be noted that the tasks parameter of waterfall can only be of array type.
When a function error occurs in the middle, its err is directly passed to the final callback, the result is discarded, and subsequent functions are no longer executed.
parallel(tasks, [callback])
The parallel function executes 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 in the order declared in the tasks, rather than the order in which execution is completed. An 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 a json object, the same as the series function,
Different tasks parameter types will result in different formats of returned results.
parallelLimit(tasks, limit, [callback])
The parallelLimit function is similar to parallel, but it has an additional parameter limit.
The limit parameter limits tasks to a certain number of concurrent tasks, rather than unlimited concurrency. The example is as follows:
async.parallelLimit([ function(callback){ callback(null, 'one'); }, function(callback){ callback(null, 'two'); } ],
2,
function(err, results){ console.log(results); });