Home > Article > Web Front-end > Detailed explanation of asynchronous processing using node Async
This time I will bring you a detailed explanation of the asynchronous processing of node Async. What are the precautions of the detailed explanation of the asynchronous processing of node Async. The following is a practical case, let's take a look.
I have been studying nodejs recently, and what makes me feel more deeply is... People on earth who are familiar with js code know that the loading order of js is very important! Very important! ! Then the question comes. When writing node, many interfaces will be requested in the background (our company transfers data with the Java background), and the interface will have a callback. What should I do with so many callbacks... Each callback returns How to deal with the loading order is a problem... It is impossible to nest... Nest... Let's do this, it will be very troublesome! ! Why is there no solution to such a big problem? That's impossible... right!
Async asynchronous processing module!
The following is little Sam’s understanding:
The installation is very simple, just like the ordinary installation module
The installation is successful! The next step is to quote . Quote the module in the code:
var async = require('async');
It’s OK to quote this way! You can easily use the async object module behind the code!
I checked the official documentation and found that there are too many methods in this module! It’s dizzying to see…
Let’s talk about a few methods that the editor thinks are more commonly used
series
1.series(tasks, [callback]) sequentially executes the functions in the array and collection. When the previous function is completed, the next function will be executed immediately. If the function triggers an error, it can be verified in the callback function, otherwise it will continue to be executed. Complete tasks
It’s useless to talk too much, code is the real father
Use case:
//tasks传的是一个数组函数 async.series([ function(callback) { // do some stuff ... callback(null, 'one'); }, function(callback) { // do some more stuff ... callback(null, 'two'); }], // optional callback //我觉得这里很重要err是上面两个函数的错误信息,而results这个参数是一个数组对象,它们的值是上面数组函数每个callback里面的参数。 function(err, results) { // results is now equal to ['one', 'two'] }); // an example using an object instead of an //下面的代码其实跟上面的差不多,只是stasks传的是对象而不是数组 async.series({ one: function(callback) { setTimeout(function() { callback(null, 1); }, 200); }, two: function(callback) { setTimeout(function() { callback(null, 2); }, 100); } }, function(err, results) { // results is now equal to: {one: 1, two: 2} });
parallel
2.parallel(tasks, [callback]) Execute methods in arrays and collections in parallel. You don’t have to wait until the previous function is executed before executing the next function. If the function triggers an error, you can verify it in the callback function
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 thoug // the second function had a shorter timeout } );
parallelLimit
3.parallelLimit(tasks, limit, [callback]) The usage is the same as 2, except that there is an additional limit on the number of tasks. The maximum number of tasks allowed to be executed in parallel
whilst
## 4.whilst(test, fn, callback) Equivalent to the usage of while, the first parameter is the verification condition, the second parameter is the execution function, and the third parameter is thecallback function after verification fails, generally It is often used when doing delayed animation
var count = 0; async.whilst( function () { return count < 5; }, //验证成 功继续,失败进回调 function (callback) { count++; setTimeout(callback, 1000); }, function (err) { // 5 seconds have passed } );
waterfall
5.waterfall(tasks, [callback]) Tasks are run in sequence. The callback of the previous function will be used as the parameter of the nextfunction. If any task passes an error callback, the next function will not be executed
async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ // arg1 now equals 'one' and arg2 nowequals 'two' callback(null, 'three'); }, function(arg1, callback){ // arg1 now equals 'three' callback(null, 'done'); } ], function (err, result) { // result now equals 'done' });
compose
6.compose(fn1, fn2...) Add to the queue in order, execute in order, use the result of the previous function as the value of the next functionfunction add1(n, callback) { setTimeout(function () { callback(null, n + 1); }, 10); } function mul3(n, callback) { setTimeout(function () { callback(null, n * 3); }, 10); } var add1mul3 = async.compose(mul3, add1); add1mul3(4, function (err, result) { // result now equals 15 });I believe you have mastered it after reading the case in this article Method, for more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
The above is the detailed content of Detailed explanation of asynchronous processing using node Async. For more information, please follow other related articles on the PHP Chinese website!