Graphical promise

Time:2021-4-18

Author: Herry lo
Blog original link

PromisesObject is used to represent the final completion (or failure) of an asynchronous operation and its result value. The main purpose is to solve the problem of asynchronous operation.

A promise object has the following three states:

Pending: initial state, neither success nor failure.
Fully (resolved): means that the operation has been completed successfully.
Rejected: means the operation failed.

A change in the internal operation of promise object is as follows:

1. When new promise() is instantiated, it means promise is in the pending initialization state, ready to run.
2. Once the project instance runs successfully or fails, the state of the instance will change to full or rejected, and the state cannot be changed.

Promise instance process

The call process of promise instance function is as follows:

Graphical promise

The internal is mainly the change of state. When the state is padding, it will wait for execution. Once the state is not padding, it will run the function stored in the array.

Run code example

Handling non asynchronous situations:

var p = new Promise(function (resolve, reject) {
    resolve('Promise');
})
p.then((result) => {
    console.log(1)
})
p.then((result) => {
    console.log(2)
});

Instantiate promise first, and execute the callback function at the same time. The status changes from pedding to full, and the execution of promise instance callback function is completed. (in this case, the then callback function will not be saved, and the function will be executed in sequence.)

Continue to execute, save the then callback, find that the promise state has changed to full, and then callback runs directly. (both then callbacks here are like this.)

Handling asynchronous situations:

var p = new Promise(function (resolve, reject) {
    setTimeout(()=> {
        console.log('setTimeout');
        resolve('Promise')
    },1000)
})
p.then((result) => {
    console.log(1)
})
p.then((result) => {
    console.log(2)
});

Instantiate promise first, and execute the callback function at the same time. Because it is a setTimeout function, the callback function enters the task queue, and the status is still pedding. The execution of the callback function of promise instance is completed, and other synchronization functions continue to be executed. (the then callback function is not saved at this time)

Continue to execute and save the then callback. It is found that the promise state is still pending. The then callback is saved in the array and the then callback is saved. Execute the setTimeout callback function to execute the then callback saved in the array.

Tips:Then / catch is a promise

This is the basic operation of promise. You can have a look if you are interestedSource code analysis of promise

reference resources:

Promise source code

ecma262: Promise Abstract Operations

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]