Home  >  Article  >  Web Front-end  >  What does es6 promise mean?

What does es6 promise mean?

WBOY
WBOYOriginal
2022-05-05 14:37:563471browse

In es6, promise means "commitment". Promise represents the result of an asynchronous operation. It is a new asynchronous programming solution. It is represented as an object in the code and is mainly used to solve the callback area problem. The syntax is "new Promise(function(resolve, reject){..})".

What does es6 promise mean?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the meaning of promise in es6

Promise means promise. The core idea behind it is that promise represents the result of an asynchronous operation.

is a new asynchronous programming solution in es6, which is represented as an object in the code.

Promise is a solution provided by js asynchronous programming, mainly used to solve the problem of callback area.

Promise has three states, namely

  • Pending (in progress) initial state

  • Fulfilled (successful) means The operation is successful

  • rejected (failed) means the operation failed

Note: The three states of the Promise object are not affected by the outside world , only events stored in the promise that will end in the future will be affected. That is to say, only the result of asynchronous operation can determine the current state, and no other operation can change this state.

Once the Promise state changes, it is irreversible.

Pending (in progress) state can Transition to the Fulfilled (successful) state

Pending (in progress) state can be transitioned to the rejected (failed) state

promise Only these two situations will change the state. Once these two situations occur If the status changes, then the status is solidified and will always maintain this result.

Basic usage

Syntax:

new Promise( function(resolve, reject) {...} /* executor */  )

Principle:

When building a Promise object, you need to pass in an executor function. The main business processes are executed in the executor function.

The executor function is called immediately when the Promise constructor is executed. The resolve and reject

functions are passed to the executor as parameters. When the resolve and reject

functions are called, respectively The promise's status changes to fulfilled or rejected. Once the state changes, it will not change again, and this result can be obtained at any time.

After calling the resolve function in the executor function, the callback function set by promise.then will be triggered; and after calling the reject

function, the callback function set by promise.catch will be triggered.

As shown in the figure below:

What does es6 promise mean?

The example is as follows:

Create a new Promise object

You need to pass in a callback function. The callback function has 2 parameters, representing resolve (resolve) and reject (reject), and both parameters are functions

If neither parameter is called, the default pending state

let promise=new Promise(function(resolve,reject){
});//pending状态

Call the resolve function, which represents the state of Promise, and will change from pending==>fulfilled

let promise=new Promise(function(resolve,reject){
resolve();
});//fulfilled状态

Call the reject function, which represents the state of Promise, and will change from pending==>rejected

let promise=new Promise(function(resolve,reject){
reject();
});//rejected 状态

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What does es6 promise mean?. 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
Previous article:Is Set for ES6?Next article:Is Set for ES6?