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 principle of promise

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what the principle of promise is. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Promise application scenarios

1 resolve callback hell

For example, we may often need to request one data asynchronously and then use it as an input parameter for the next asynchronous operation.

GetData (function (a) {getMoreData (a, function (b) {getMoreData (b, function (c) {getMoreData (c, function (d) {getMoreData (d, function (e) {...});})

You can find that the above code looks very scary, layers of nesting, if you add complex logical judgment, the readability of the code will become very poor.

But if you use promise:

Function getData () {return new Promise (function (resolve, reject) {resolve (1);});} function getMoreData (arg) {return new Promise (function (resolve, reject) {resolve (arg + 10);});} getData (). Then (function (a) {console.log (a); / / 1 return getMoreData (a);}) .then (function (b) {console.log (b); / / 11})

Make the above code a little more concise

GetData () .then (a = > getMoreData (a)) .then (b = > console.log (b))

2 promise can get or process a result after multiple requests have been sent.

/ / after both data are returned, let fs = require ('fs'); fs.readFile ('. / 1.txtbread, 'utf8', function (err, data) {console.log (data);}) fs.readFile ('. / 2.txtspell, 'utf8', function (err, data) {console.log (data);}) can be implemented using promise: let fs = require (' fs') Function read (url) {return new Promise (function (resolve,reject) {fs.readFile (url,'utf8',function (err,data) {if (err) reject (err); resolve (data);})} Promise.all ([read ('1.txt'), read (' 2.txt')]) .then (data= > {console.log (data);}, err= > {console.log (err);})

Second, the realization of promise principle

1. The simplest implementation

Based on the above application scenario, it is found that promise can have three states, namely pedding, Fulfilled and Rejected.

The initial state of the Pending Promise object instance when it was created

Fulfilled can be understood as the state of success.

Rejected can be understood as the state of failure.

To construct an Promise instance, you need to pass a function to the Promise constructor. The function passed in needs to have two formal parameters, both of which are of type function. They are resolve and reject.

There is also a then method on Promise. The then method is used to specify the action to be performed when the state of the Promise object changes. The first function (onFulfilled) is executed when resolve and the second function (onRejected) is executed when reject.

When the state changes to resolve, it can no longer become reject, and vice versa.

Based on the above description, we can implement a promise like this.

Function Promise (executor) {/ / executor Actuator let self = this; self.status = 'pending'; / / waiting self.value = undefined; / / indicates the value of current success self.reason = undefined; / / indicates the value of failure function resolve (value) {/ / successful method if (self.status =' pending') {self.status = 'resolved' Self.value = value;}} function reject (reason) {/ / failed method if (self.status = 'pending') {self.status =' rejected'; self.reason = reason;}} executor (resolve,reject);} Promise.prototype.then = function (onFufiled,onRejected) {let self = this If (self.status = = 'resolved') {onFufiled (self.value);} if (self.status =' rejected') {onRejected (self.reason);}} module.exports = Promise; the above is what the promise principle is. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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