This is the highlight of handwritten Promsie, so for those of you who haven’t seen my handwritten Promise, please finish reading it first

Brave ape ape, not afraid of difficulties!! — Handwritten Promise (part 1)

highlight

  • Immediately after we finished writing about the basic features of promises last time, this article starts with calls.thenMethod returns a new Promise instance and other advanced Promise features
  • .thenOnfulfilled and onRejected are two function parameters (onfulfilled and onRejected), which are called when the Promise instance succeeds or fails. The successful execution of these two functions also determines the state of the new instance returned. As soon as these two functions fail, the new instance will execute rejec and fail.
  • Also, the state of the new Promise instance depends on whether a new Promise instance is returned
  • .thenIf the function fails to pass the first function argument or the second function argument, the system passes a function argument by default. If the function fails to pass the first function argument, the system passes a function argument directly. If the function fails to pass the second function argument, the system passes an exception, and the next function can be used.thenContinue to perform
Promise.prototype = {
          // Whether it is custom
          customize: true.constructor: Promise.// Then is executed asynchronously and returns a new Promise instance
           then: function (onfulfilled, onrejected) {
            // This is a big pity /onrejected
            if (typeofonfulfilled ! = ='function') {
              onfulfilled = function onfulfilled(value) {
                return value;
              };
            }
            if (typeofonrejected ! = ='function') {
              onrejected = function onrejected(reason) {
                throw reason;
              };
            }
			//self is the original Promise instance
            var self = this;
            // Promise is the new promise instance
            var promise=new Promise(function(resolve,reject){
            	switch (self.PromiseState) {
                // Know the state
                case 'fullfilled':
                  setTimeout(function () {
                    try {
                      var x = onfulfilled(self.PromiseResult);
                      resolvePromise(promise, x, resolve, reject);
                      // Determine if x is a Promise instance
                    } catch (err) {
                      // Execute reject if there is an errorreject(err); }});break;
                case 'rejected':
                  setTimeout(function () {
                    try {
                      var x = onrejected(self.PromiseResult);
                      resolvePromise(promise, x, resolve, reject);
                    } catch(error) { reject(error); }});break;
                /* When we do not know the state of the instance (which is an asynchronous operation in executor), we should store methods passed in then and tell them to execute */ when we execute resolve/reject
                default:
                  // self.onFulfilledCallbacks.push(onfulfilled);
                  // self.onRejectedCallbacks.push(onrejected);
                  // This is a big mistake. // This is a big mistake. // This is a big mistake
                  self.onFulfilledCallbacks.push(function (PromiseResult) {
                    try {
                      var x = onfulfilled(PromiseResult);
                      resolvePromise(promise, x, resolve, reject);
                    } catch(error) { reject(error); }}); self.onRejectedCallbacks.push(function (PromiseResult) {
                    try {
                      var x = onrejected(PromiseResult);
                      resolvePromise(promise, x, resolve, reject);
                    } catch(error) { reject(error); }});break; }})// Return a new promise instance
            returnpromise; }}Copy the code
  • This uniformly handles the success and failure of returning a new instance based on THEN

  • First check whether the returned value is the same as the newly created instance. If so, an infinite loop will occur.

  • According to the Promise A+ specification, there are several conditions for whether or not it is A Promise instance

    • It must be a function or an object
    • There must be.thenmethods
  • If the. Then method returns two function parameters that do not return a PROMISE instance, then the new promise instance becomes successful. If it is a Promise instance, you must determine whether the above conditions are met. The state of the new Promise instance is then determined by executing resolve/ Reject

/ * promise: to deal with the promise of instance x: implement onfulfilled/onrejected resolve: getting the result of the promsie become successful reject: * / promise change failure
 // Uniformly handles success and failure of new instances based on THEN
        function resolvePromise(promise, x, resolve, reject) {
          // If the return value is the same as the newly created instance, an infinite loop is formed
          if (x === promise) {
            throw new TypeError('Chaining cycle detected for promise #<Promise>');
          }
          if((x ! = =null && typeof x === 'object') | |typeof x === 'function') {
            try {
              var then = x.then;
              if (typeof then === 'function') {
                At this point, the return result must be a new promsie instance
                then.call(
                  x,
                  function (y) {
                    resolve(y);
                  },
                  function (r) { reject(r); }); }else{ resolve(x); }}catch(error) { reject(error); }}else{ resolve(x); }}Copy the code