In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "how to use the Promise object in ES6", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Promise objects in ES6" can help you solve your doubts.
The way asynchronous callbacks are handled before promise
Function asyncFun (function () {callback (aqumb);}, 200);} asyncFun (1Power2, function (res) {if (res > 2) {asyncFun (res, 2, function (res) {if (res > 4) {asyncFun (res, 2, function (res) {console.log ('ok'); console.log (res)) })}))
From the above, we can see the horror of the so-called "callback hell".
Use promise to handle async gracefully
Function asyncFun (resolve, reject) {return new Promise (function (resolve, reject) {setTimeout (function () {resolve (a + b);}, 200);})} asyncFun (1Power2). Then (function (res) {if (res > 2) {return asyncFun (res, 2);}}) .then (function (res) {if (res > 4) {return asyncFun (res, 2);}}) .then (function (res) {console.log ('ok')) Console.log (res);}) .catch (function (error) {console.log (error);})
An example of using promise to handle internal exceptions
Function asyncFun (resolve, reject) {return new Promise (function (resolve, reject) {/ / Simulation exception judgment if (typeof a! = = 'number' | | typeof b! = =' number') {reject (new Error ('no number'));} setTimeout (function () {resolve (a + b);}, 200);} asyncFun (1J2). Then (function (res) {if (res > 2) {return asyncFun (res, 2) }, function (err) {console.log ('first err:', err);}) then (function (res) {if (res > 4) {return asyncFun (res,'a');}}, function (err) {console.log ('second err:', err);}) .then (function (res) {console.log ('ok'); console.log (res);}, function (err) {console.log (' third err:', err);})
From the above, we can see that the exception in the promise object is handled by the second callback function of then, and the exception promise object is returned through reject.
Errors are handled uniformly through catch, and logic that must eventually be executed is executed through finally
Function asyncFun (resolve, reject) {return new Promise (function (resolve, reject) {/ / Simulation exception judgment if (typeof a! = = 'number' | | typeof b! = =' number') {reject (new Error ('no number'));} setTimeout (function () {resolve (a + b);}, 200);} asyncFun (1J2). Then (function (res) {if (res > 2) {return asyncFun (res, 2) }) .then (function (res) {if (res > 4) {return asyncFun (res,'a');}}) .then (function (res) {console.log ('ok'); console.log (res);}) .catch (function (error) {console.log (' catch:', error);}) .finally (function () {console.log ('finally:', 1x2);})
Handle multiple asynchronies through the Promise.all () static method
Function asyncFun (resolve, reject) {return new Promise (function (resolve, reject) {setTimeout (function () {resolve (a + b);}, 200);})} var promise = Promise.all ([asyncFun (1), asyncFun (2), asyncFun (3)]) promise.then (function (res) {console.log (res); / / 3, 5, 7]})
Get the fastest of multiple asynchronies through the Promise.race () static method
Function asyncFun (resolve, reject) {return new Promise (function (resolve, reject) {setTimeout (function () {resolve (a + b);}, time);})} var promise = Promise.race ([asyncFun (1) 2), asyncFun (2), asyncFun (3)]) promise.then (function (res) {console.log (res); / / 5})
Return the successful asynchronous object directly through the Promise.resolve () static method
Var p = Promise.resolve ('hello'); p.then (function (res) {console.log (res); / / hello})
Equivalent to, as follows:
Var p = new Promise (function (resolve, reject) {resolve ('hello2');}) p.then (function (res) {console.log (res); / / hello2})
Return the failed asynchronous object directly through the Promise.reject () static method
Var p = Promise.reject ('err') p.then (null, function (res) {console.log (res); / / err})
Equivalent to, as follows:
Var p = new Promise (function (resolve, reject) {reject ('err2');}) p.then (null, function (res) {console.log (res); / / err})
Use a small example to test the application of Promise in object-oriented
'use strict';class User {constructor (name, password) {this.name = name; this.password = password;} send () {let name = this.name; return new Promise (function (resolve, reject) {setTimeout (function () {if (name =' leo') {resolve ('send success');} else {reject (' send error');}});} validatePwd () {let pwd = this.password Return new Promise (function (resolve, reject) {setTimeout (function () {if (pwd = '123') {resolve ('validatePwd success');} else {reject (' validatePwd error');}})} let user1 = new User ('Joh'); user1.send () .then (function (res) {console.log (res);}) .catch (function (err) {console.log (err);}) Let user2 = new User ('leo'); user2.send () .then (function (res) {console.log (res);}) .catch (function (err) {console.log (err);}); let user3 = new User (' leo', '123'); user3.validatePwd () .then (function (res) {return user3.validatePwd ();}) .then (function (res) {console.log (res)) ) .catch (function (error) {console.log (error);}); let user4 = new User ('leo',' 1234'); user4.validatePwd () .then (function (res) {return user4.validatePwd ();}) .then (function (res) {console.log (res);}) .catch (function (error) {console.log (error);}) After reading this, the article "how to use Promise objects in ES6" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. 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: 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.
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.