SoFunction
Updated on 2025-03-01

Detailed explanation of the async implementation process of Javascript asynchronous programming

async official DOC

introduce

node installation

npm install async --save

use

var async = require('async')

js file

/caolan/async/tree/master/dist

async provides many functions for asynchronous process control. Below are several functions in the core async. Please see the complete functions.async official DOC

(['file1','file2','file3'], , function(err, results) {
  // results is now an array of stats for each file
});

(['file1','file2','file3'], function(filePath, callback) {
 (filePath, function(err) {
  callback(null, !err)
 });
}, function(err, results) {
  // results now equals an array of the existing files
});

([
  function(callback) { ... },
  function(callback) { ... }
], function(err, results) {
  // optional callback
});

([
  function(callback) { ... },
  function(callback) { ... }
]);

series serial

The function of series is to execute it in sequence at one time.

([
 function(callback) {
  setTimeout(function() {
   callback(null, 1)
    }, 2000);
  },
 function(callback) {
  callback(null, 2);
 }],
 function(err, results) {
  (results);
 });

The output result is

[ 1, 2 ]

The first parameter of the series function can be an array or a JSON object. The parameter types are different, which affects the format of the return data.

({
	one: function(callback){
		callback(null, 1);
	},
	two: function(callback){
		callback(null, 2);
	}
},function(err, results) {
	(results);
});

The output is

{one: 1, two: 2}

waterfall waterfall flow

The waterfall and series functions are executed in order. The difference is that the value generated by each function of waterfall can be passed to the next function, but series cannot.

([
	function(callback) {
		callback(null, 'one', 'two');
	},
	function(arg1, arg2, callback) {
		// arg1 now equals 'one' and arg2 now equals 'two'
		('function 2')
		('arg1: ' + arg1)
		('arg2: ' + arg2)
		callback(null, 'three');
	},
	function(arg1, callback) {
		('function 3')
		('arg1: ' + arg1)
			// arg1 now equals 'three'
		callback(null, 'done');
	}
], function(err, result) {
	// result now equals 'done'
	(result);
});

Output

function 2
arg1: one
arg2: two
function 3
arg1: three
done

The first parameter of waterfall can only be an array. When a function error occurs in the middle, its err is passed directly to the final callback, and the result is discarded and the subsequent function will no longer be executed.

parallel(tasks, [callback])

The parallel function executes multiple functions in parallel, each function is executed immediately, and there is no need to wait for other functions to execute first.
The data in the array passed to the final callback is in the order declared in the tasks, not in the order in which the execution is completed.

([
  function(callback){
    callback(null, 'one');
  },
  function(callback){
    callback(null, 'two');
  }
],
function(err, results){

});

The tasks parameter can be an array or a json object. Like the series function, the task parameter types are different, and the results format returned will be different.

In the example, call the tasks callback function with setTimeout after 1000 milliseconds, and then output results in the parallel callback function to see if the whole process takes 1s or 2s.

var async=require("async");
([
  function(callback){
    setTimeout(function(){
      callback(null, 'one')
    },1000);
  },
  function(callback){
    setTimeout(function(){
      callback(null, 'two')
    },1000);
  }
],
function(err, results){
  (results);
});

parallelLimit(tasks, limit, [callback])

parallelLimit function is similar to parallel, but it has one more parameter limit. The limit parameter limits tasks can only be concurrent at a certain number of times, rather than unlimited concurrency. The example is as follows:

([
  function(callback){
    callback(null, 'one');
  },
  function(callback){
    callback(null, 'two');
  }
],
2,
function(err, results){
});

In the example, the callback function of tasks in the tasks is called after 1000 milliseconds, the limit parameter is set to 1, and then the results are output in the callback function of parallelLimit, and see if the whole process takes 1s or 2s.

var async=require("async");
([
  function(callback){
    setTimeout(function(){
      callback(null, 'one');
    }, 1000);
  },
  function(callback){
    setTimeout(function(){
      callback(null, 'two');
    }, 1000);
  }
],
1,
function(err, results){
  (results);
});

map(coll, iteratee, callbackopt)

The map function traverses the passed array and performs the same operation, and finally returns the result

cool:array

iteratee: The function to be executed for each item in the array

callbackopt: callback function

([1, 2, 3, 4, 5],
	function(item, callback) {
		callback(null, item + 5)
	},
	function(err, result) {
		(result)
	})

[ 6, 7, 8, 9, 10 ]

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.