Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Promise in Javascript

Shulou Source: shulou.com Published: 2022-06-01 13:18:03 09月24日 Update

In this article, the editor introduces in detail "how to use Promise in Javascript", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Promise in Javascript" can help you solve your doubts.

Method 1: handle asynchronous callback

For the basic use of Promise in JavaScript to handle asynchronous callbacks, let's take a look at the following code:

Function Pro1 () {return new Promise (function (resolve, reject) {setTimeout (function () {resolve ('pro1')}, 300)} / / call Pro1 () .then (function (data) {console.log (data) / / pro1}) .catch (function (err) {throw new Error (err)})

Method 2: synchronous processing of multiple asynchronous functions

So in development, sometimes we need to send two Ajax, we hope they can return the data, we can use the following method, the code is as follows:

Function Pro1 () {return new Promise (function (resolve, reject) {setTimeout (function () {resolve ('pro1')}, 300)} function Pro2 () {return new Promise (function (resolve, reject) {setTimeout (function () {resolve (' pro2')}, 300)})} / / call var Pro = Promise.all ([Pro1 (), Pro2 ()]) Pro.then (function (data) {console.log (data [0], data [1]) / / Pro1 Pro2}) .catch (function (err) {throw new Error (err)})

Method 3: asynchronously dependent asynchronous callback

In this method, we can use this method to refer to the following code, which is as follows:

Function Pro1 (orderId) {return new Promise (function (resolve, reject) {setTimeout (function () {var orderInfo = {orderId: orderId, productIds: ['123,' 456']} resolve (orderInfo.productIds)}, 300)} function Pro2 (productIds) {return new Promise (function (resolve, reject) {setTimeout (function () {var products = productIds.map (function (productId) {return {productId: productId) Name: 'clothes'}) resolve (products)}, 300)} / / call Pro1 ('abc123'). Then (function (productIds) {console.log (' commodity id',productIds) return Pro2 (productIds)}) .then (function (products) {console.log ('commodity details', products)}) .catch (function (err) {throw new Error (err)})

Method 4: encapsulate the unified entrance method or error handling

The error handling code is as follows:

Function ErrorHandler (promiseObj, rejectOrResOrCallback) {return promiseObj.then (null, function (err) {if (! err)})} here, this article "how to use Promise in Javascript" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.

Tags: Methods processing code articles content goods errors propriety two entries functions methods multiple ideas data new knowledge more sometimes steps knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Xiaomi MySQL Docker