Home >Web Front-end >JS Tutorial >How to use async in Node.js

How to use async in Node.js

不言
不言Original
2019-01-03 13:37:474596browse

async in Node.js can use readable code to implement asynchronous processing. Asynchronous processing is the process of comparing data before and after updating and transmitting data only when the page is updated. This asynchronous processing Processing is sometimes called Ajax in front-end processing.

How to use async in Node.js

Promise and async are used as asynchronous processing implementation methods

You can use promise to asynchronously process Node.js.

In fact, it is easier to implement asynchronous processing than to implement asynchronous processing.

However, asynic has Internet browsers and other incompatible browsers, so we need to think carefully in practice.

How to use async in Node.js

How to install async package

Command

npm install async

Async processing syntax

async.方法名称([
  function(callback) {
    内容处理
  }
});

Analysis:

The async. method name in line 1 specifies an asynchronous method Name to call the handler method.

The functin (callback) in line 2 is processed according to the parameters of the callback part.

The processing content of line 3 is to complete the actual processing.

Line 4 ends processing.

Typical example of async processing method

waterfall

Use an array to perform asynchronous processing.

It is executed according to the order of the array.

series

also uses an array to perform asynchronous processing and executes it according to the order of the array.

The difference from waterfall is that when you perform the following processing, you need to call the callback function to continue processing.

The values ​​passed as arguments to this callback function are assigned to the array in order at the end of processing.

parallel

parallel handles parallelism, that is, it cannot be executed sequentially like a series.

The callback function is called in parallel to continue processing. The value passed to this callback function as a parameter is also assigned to the array at the end of processing.

The above is the detailed content of How to use async in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn