In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
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: 215
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.