preface

As a front-end developer, Promises have become a powerful tool in the launch/production process, and I’ve read a lot of articles about them online, followed the lead of the big guns and sneaky notes. Here the author systematically summarized how to step by step single self Promise source code, let the reader in the interview process how easy to deal with.

Of course, you can’t eat a Promise in one bite. Be clear about what promises are, what implementation criteria are, and how to implement them according to those criteria.

This article is the first article of the single lu Promise source, aimed at Promises/A + implementation of the standards for the translation. Hope to be helpful to you as you read.

For the official document, please make Promises/A+.

1. The term

1.1. Promise is an object or function that has a THEN method, and the behavior follows this specification

1.2. thenable is an object or function that has a THEN method

1.3. Value is the successful value of the promise state (including undefined/thenable or promise)

1.4. An exception is an outlier thrown by a throw

Reason is the value when the promise state fails

2. Request

2.1 Promise States

A Promise must be in one of three states: pending, fulfilled, or rejected

  • 2.1.1 If the Promise is in the Pending State
This may become a pityCopy the code
  • 2.1.2 If the promise is fulfilled
2.1.2.1 Does not change to another state 2.1.2.2 Must have a valueCopy the code
  • 2.1.3 If the Promise is in the Rejected state
2.1.3.1 Do not change to another state 2.1.3.2 There must be a Reason to reject a promiseCopy the code

This is very depressing. This is very depressing. This is very depressing. Promise have the value of success. Promise failed, and failed again.

2.2 then method

The PROMISE must provide a THEN method to access the final result. The PROMISE’s THEN method takes two arguments

promise.then(onFulfilled, onRejected)
Copy the code
  • 2.2.1 onFulfilled and onrejected are optional parameters
2.2.1.1 onFulfilled must be a function type. If it is not, it is ignored. 2.2.1.2 onRejected must be the function type. If it is not, ignore it.Copy the code
  • 2.2.2 If onFulfilled is a function
2.2.2.1 onFulfilled must be used when the promise becomes fulfilled. This parameter is the value of the promise 2.2.2.2 Before the promise state is not fulfilled 2.2.2.3 onRejected can only be called onceCopy the code
  • This is very depressing and onRejected
  • 2.2.5 onFulfilled and onRejected must be called as functions
  • 2.2.6 The THEN method may be called multiple times
2.2.6.1 If the promise becomes fulfilled, all onFulfilled pullbacks shall be fulfilledthen2.2.6.2 If a Promise is in the Rejected state, all onRejected callbacks need to bethenSequential execution ofCopy the code
  • 2.2.7 Then must return a promise
promise2 = promise1.then(onFulfilled, onRejected);
Copy the code
[[Resolve]](promise2, x) [[Resolve]](promise2, x) This will be fulfilled fulfilled 2.2.7.3 if onRejected is not a function, then the value of promise2 will be fulfilled 2.2.7.4 if onRejected is not a function, then the value of promise2 will be fulfilled. Promise2 iS promise1 the reason rejectedCopy the code

[[Resolve]](promise, x

  • 2.3.1 Reject the promise with a TypeError if the promise is equal to x
  • 2.3.2 If X is a Promise
2.3.2.1 If x is in the pending state, the promise must be pending until X becomes fulfilled or rejected. 2.3.2.3 If X is rejected, reject the promise with the same reasonCopy the code
  • 2.3.3 If x is an object or a function
2.3.3.1  let then= x.tehen 2.3.3.2 Reject promise with eas the reason if the result of x.tehen throws an error E 2.3.3.3 IfthenIs a function, Then. Call (x, resolvePromise, rejectPromise) 2.3.3.3.1 resolvePrmoise with input y, execute [[Resolve]](promise, Y) 2.3.3.3.2 Reject Promise with r, rejectPromise with r 2.3.3.3.3 If both resolvePromise and rejectPromise are called, then the first call takes priority, Later calls ignore 2.3.3.3.4 if calledthenIf either resolvePromise or rejectPromise has already been called, then ignore 2.3.3.3.4.2 otherwise, Reject promise with eas the reason 2.3.3.4 IfthenIs not afunctionFulfill promise with XCopy the code
  • If x is not an object or a function, fulfill promise with x

conclusion

Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ : Promises/A+ Here I will implement A variety of Promises based on Promises/A+ core standard.

Point:

1.Promise and asynchronous programming (2) : Single-haned simplified version Promise

Like, also hope to a small wave of quality three consecutive (like comment + attention), I will continue to work hard! Ongoing updates..