Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the status of ES6 Promise asynchronous operations

Shulou Source: shulou.com Published: 2022-06-01 10:56:59 09月26日 Update

This article focuses on "what is the status of ES6 Promise asynchronous operations", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the status of ES6 Promise asynchronous operations?"

Overview

Is a solution to asynchronous programming. Syntactically, Promise is an object from which you can get messages for asynchronous operations.

Promise statu

There are three states for Promise asynchronous operations: pending (in progress), fulfilled (successful), and rejected (failed). Nothing can change this state except as a result of an asynchronous operation.

The Promise object only has a state change from pending to fulfilled and from pending to rejected. As long as you are in fulfilled and rejected, the state will not change again, that is, resolved.

Const p1 = new Promise (function (resolve,reject) {resolve ('success1'); resolve (' success2');}); const p2 = new Promise (function () {resolve ('success3'); reject (' reject');}); p1.then (function (value) {console.log (value); / / success1}); p2.then (function (value) {console.log (value); / / success3}); shortcomings of state

Cannot cancel Promise, as soon as it is newly created, it will be executed immediately, and cannot be cancelled halfway.

If the callback function is not set, the errors thrown inside the Promise will not be reflected externally.

When you are in the pending state, there is no way to know what stage of progress you are currently at (just beginning or nearing completion).

Then method

The then method takes two functions as arguments, the first is the callback if the Promise executes successfully, and the second is the callback if the Promise fails, and only one of the two functions will be called.

Characteristics of then method

The callback function will never be called until the current run of the JavaScript event queue is complete.

Const p = new Promise (function (resolve,reject) {resolve ('success');}); p.then (function (value) {console.log (value);}); console.log (' first'); / / first// success

Callback functions added in the form of. Then are called at any time.

By calling .then multiple times, you can add multiple callback functions that run independently in the insertion order.

The then method returns a Promise object with a resolved or rejected state for chained calls, and the value of the Promise object is the return value.

Attention points of then method

Simple Promise chain programming is best kept flat and not nested Promise.

Note that the Promise chain is always returned or terminated.

At this point, I believe you have a deeper understanding of "what is the status of ES6 Promise asynchronous operations?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: State function method object parameter success two content chain learning programming running practical deeper simple event interest only multiple practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Docker Apple Shulou Information