How to replace the callback function with promise in node
This time I will bring you how to replace the callback function with promise in node, how to replace the callback function with promise in nodeWhat are the precautions, the following is a practical case, one Get up and take a look.
In the process of learning Node.js, I came into contact with how to use async to control concurrency (use async to control concurrency)
The essence of async is a process control. In fact, in asynchronous programming, there is a more classic model called the Promise/Deferred model (of course there are more related solutions, such as eventproxy, co, etc., you will be digging holes when you encounter it)
First, let's think about a typical asynchronous programming model, consider this topic: read a file, and output the content of the file on the console
1 2 3 4 |
|
It seems very simple, go one step further: read two files, in The console outputs the contents of these two files
1 2 3 4 5 6 7 |
|
What if more files are read
1 2 3 4 5 6 7 8 9 10 |
|
This is the legendary callback hell. You can use async to improve this code, but in this case In the example we need to use promise/defer to improve it
promise Basic concept
First of all, it is an object. It is no different from ordinary JavaScript objects. At the same time, it is also a specification. It agrees with a unified interface for asynchronous operations and represents the final result of an asynchronous operation. The code is written in a synchronous manner and the operations performed are asynchronous, but It also ensures that the order of program execution is synchronous
1. Promise has only three states, unfulfilled, completed (fulfilled) and failed (rejected)
2. The state of promise can be unfulfilled Convert to completed, or incomplete to failure
3. The state transition of promise only occurs once
promise has a then method, and the then method can accept 3 functions as parameters. The first two functions correspond to the callback functions of the two states of promise: fulfilled and rejected. The third function is used to process progress information
In order to understand it, some important principles must be remembered: .then() always returns a new promise, such as the following code:
1 2 |
|
Here The parameters readAnotherFile and console.error of then represent the action onFulfilled after the asynchronous operation succeeds or the action OnRejected after the failure. That is to say, the readAnotherFile function is executed after the file is successfully read, otherwise an error is printed and recorded upon failure. This implementation is only one of the two possible
It can also be understood as:
1 2 3 4 5 6 7 |
|
The two parts of the Promise law must be separated:
1. then() is always Returns a new promise every time you call it, it doesn't matter what the callback does, because .then() has given you a promise before the callback is called, the behavior of the callback only affects the implementation of the promise, if the callback returns a value, then the promise will use that value. If the value is a promise, return the value after the promise is implemented to this value. If the callback throws an error, the promise will reject the error
2. By .then() The returned promise is a new promise, which is different from the promise on which .then() was called. Long promise chains sometimes hide the fact that each time .then() is called, a new promise is generated. , it must be noted here that what you really need to consider is that your last call to .then() may represent a failure. If you do not catch this failure, it will easily cause your error exception to disappear.
Let’s look at an exploit q A simple example to handle this problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Passing of promise
#then method will return a promise, in the following example , we use outputPromise to point to the promise returned by then.
1 2 3 4 5 |
|
Now outputPromise becomes a promise controlled by function(fulfilled) or function(rejected). The straightforward meaning is: when function(fulfilled) or function(rejected) returns a value, such as a string, array, object, etc., then the status of outputPromise will become fulfilled.
In the following example, we can see that when we change the status of inputPromise to fulfilled through defer.resovle(), the console outputs fulfilled.
When we change the status of inputPromise to fulfilled The status changes to rejected through defer.reject(), and the console outputs rejected
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
当 function(fulfilled) 或者 function(rejected) 抛出异常时,那么 outputPromise 的状态就会变成 rejected
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
当 function(fulfilled) 或者 function(rejected) 返回一个 promise 时,outputPromise 就会成为这个新的 promise.
这样做的意义在于聚合结果 (Q.all),管理延时,异常恢复等等
比如说我们想要读取一个文件的内容,然后把这些内容打印出来。可能会写出这样的代码:
1 2 3 4 5 6 |
|
然而这样写是错误的,因为 function(fulfilled) 并没有返回任何值。需要下面的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
方法传递
方法传递有些类似于 Java 中的 try 和 catch。当一个异常没有响应的捕获时,这个异常会接着往下传递
方法传递的含义是当一个状态没有响应的回调函数,就会沿着 then 往下找
没有提供 function(rejected)
1 |
|
如果 inputPromise 的状态由未完成变成 rejected, 此时对 rejected 的处理会由 outputPromise 来完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
没有提供 function(fulfilled)
1 |
|
如果 inputPromise 的状态由未完成变成 fulfilled, 此时对 fulfil 的处理会由 outputPromise 来完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
可以使用 fail(function(error)) 来专门针对错误处理,而不是使用 then(null,function(error))
1 |
|
看这个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
可以使用 progress(function (progress)) 来专门针对进度信息进行处理,而不是使用 then(function (success) { }, function (error) { }, function (progress) { })
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
promise 链
promise 链提供了一种让函数顺序执行的方法
函数顺序执行是很重要的一个功能。比如知道用户名,需要根据用户名从数据库中找到相应的用户,然后将用户信息传给下一个函数进行处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
我们通过两个 then 达到让函数顺序执行的目的。
then 的数量其实是没有限制的。当然,then 的数量过多,要手动把他们链接起来是很麻烦的。比如
1 |
|
这时我们需要用代码来动态制造 promise 链
1 2 3 4 5 6 |
|
当然,我们可以再简洁一点
1 2 3 4 |
|
看一个具体的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
对于 promise 链,最重要的是需要理解为什么这个链能够顺序执行。如果能够理解这点,那么以后自己写 promise 链可以说是轻车熟路啊
promise 组合
回到我们一开始读取文件内容的例子。如果现在让我们把它改写成 promise 链,是不是很简单呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
很有成就感是不是。然而如果仔细分析,我们会发现为什么要他们顺序执行呢,如果他们能够并行执行不是更好吗? 我们只需要在他们都执行完成之后,得到他们的执行结果就可以了
我们可以通过 Q.all([promise1,promise2...]) 将多个 promise 组合成一个 promise 返回。 注意:
1. 当 all 里面所有的 promise 都 fulfil 时,Q.all 返回的 promise 状态变成 fulfil
2. 当任意一个 promise 被 reject 时,Q.all 返回的 promise 状态立即变成 reject
我们来把上面读取文件内容的例子改成并行执行吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
现在知道 Q.all 会在任意一个 promise 进入 reject 状态后立即进入 reject 状态。如果我们需要等到所有的 promise 都发生状态后(有的 fulfil, 有的 reject),再转换 Q.all 的状态, 这时我们可以使用 Q.allSettled
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
结束 promise 链
通常,对于一个 promise 链,有两种结束的方式。第一种方式是返回最后一个 promise
如 return foo().then(bar);
第二种方式就是通过 done 来结束 promise 链
如 foo().then(bar).done()
为什么需要通过 done 来结束一个 promise 链呢? 如果在我们的链中有错误没有被处理,那么在一个正确结束的 promise 链中,这个没被处理的错误会通过异常抛出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
附录:一个 Promise 简单的应用(Node.js笔记(5)promise)
附:Promises/A+ 规范
promise 代表一个异步操作的最终结果。主要通过 promise 的 then 方法订阅其最终结果的处理回调函数,和订阅因某原因无法成功获取最终结果的处理回调函数。
更对详细见:Promises/A+
A 与 A+ 的不同点
A+ 规范通过术语 thenable 来区分 promise 对象
A+ 定义 onFulfilled/onRejectd 必须是作为函数来调用,而且调用过程必须是异步的
A+ 严格定义了 then 方法链式调用时,onFulfilled/onRejectd 的调用顺序
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to replace the callback function with promise in node. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
