Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the status of ES6 Promise asynchronous operations

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report