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

How to use Promise object in ES6

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use Promise object in ES6, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this ES6 article on how to use Promise object, let's take a look at it.

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);}) This is the end of the article on "how to use Promise objects in ES6". Thank you for reading! I believe you all have some knowledge about "how to use Promise objects in ES6". If you want to learn more, you are welcome to 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.

Share To

Development

Wechat

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

12
Report