Home > Web Front-end > JS Tutorial > What does promise mean?

What does promise mean?

(*-*)浩
Release: 2019-06-18 11:08:07
Original
22238 people have browsed it

js is single-threaded, and Promise solution is an idea of ​​asynchronous programming, which is much more powerful than traditional callback functions and events. Promise is equivalent to a placeholder for the result of an asynchronous operation. It does not subscribe to an event, nor does it pass a callback function to the target function. Instead, the function returns a Promise (equivalent to an order number).

What does promise mean?

Promise: English promise meaning, the Promise object stores the result of an event (usually an asynchronous operation) that will end in the future.

Promise has three states: (Recommended learning: Javascript video tutorial)

pendding,rejected,resolved
Copy after login

There are only two possibilities for switching the state of the Promise object:

pendding->rejected,pendding->resolved
Copy after login

Basic usage:

new Promise((resolved,rejected)=>{})
Copy after login

Promise object resolved and rejected functions. When the asynchronous event state pendding->resolved callback is successful, the resolved function will be called; when the asynchronous operation fails, the rejected function will be called.

The then(resolved,rejected) function parameter of Promise has two parameters, one resolved function and one rejected function.

Promise's catch(): Capture promise error function, which has the same function as rejected in the then function parameter. It handles errors. Since the error thrown by Promise has a bubbling nature, it can be continuously passed and will be passed to catch, so It is recommended that all error handling be placed in catch, and only successful ones will be handled in then.

One of the great features of Promise is that it can be called in a chain, and Promise objects can be returned in then and catch.

Promise.all([promise1,promise2]): The parameter is an array of promise objects. When the status of all promise objects is resolved, the status of the object will be resolved, and then will be called immediately. When a promise object is rejected, The status of the object will be rejected and the catch will be executed.

Promise.race([promise1,promise2]): racing function. When the state of a promise object changes, the object will adopt the same state and execute the corresponding function.

The life cycle of promise

Each Promise will go through a short life cycle, initially in a pending state, which means that the asynchronous operation has not yet ended. A pending Promise is also considered unsettled. Once the asynchronous operation ends, the Promise is considered settled and enters one of two possible states:

1. Fulfilled: The asynchronous operation of the Promise has succeeded End;

2. Rejected: The asynchronous operation of Promise did not end successfully, which may be an error or caused by other reasons.

Once the state changes, it is "solidified" and will remain in this state and will not change again. When the state changes, the function bound to promise.then will be called. Note: Once a Promise is created, it will be "executed immediately" and cannot be canceled. This is also one of its shortcomings.

For more technical articles related to Javascript, please visit the js tutorial column to learn!

The above is the detailed content of What does promise mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template