PromiseA+ specification

Time:2022-9-21

PromiseA+ specification

the term

  1. A promise is an object or function with a then method, and its behavior follows the PromiseA+ specification
  2. thenable is an object or function with a then method
  3. value is the value when the Promise state is successful, and is also the parameter of resolve, including various data types, undefined / string / number / boolean / promise …
  4. reason is the value when the Promise state fails, that is, the parameter of reject, indicating the reason for rejection
  5. exception throw throws the exception

specification

Promise States

There are three states of promise, pay attention to the relationship between them

  1. pending
    1.1 Initial state, can be changed
    1.2 A promise is in this state before it is resolved or rejected
    1.3 Via resolve -> fulfilled
    1.4 Through reject -> rejected
  2. fulfilled
    2.1 Final state, cannot be changed
    2.2 A promise becomes this state after being resolved
    2.3 must have a value // undefined
  3. rejected
    3.1 Final state, cannot be changed
    3.2 A promise becomes this state after being rejected
    2.3 There must be a reason value // undefined

pending -> resolve(value) -> fulfilled
pending -> reject(reason) -> rejected

then

The promise should present a then method to access the final result, be it value or reason.

    promsie.resolve(onfulfilled, onrejected);
  1. Parameter requirements
    1.1 onfulfilled must be a function type, if it is not a function, it should be ignored
    1.2 onrejected must be a function type, if it is not a function, it should be ignored
  2. onfulfilled feature
    2.1 After promise becomes fulfilled, onfulfilled should be called, the parameter is value
    2.2 It should not be called until the promise becomes fulfilled
    2.3 Can only be executed once
  3. onrejected feature
    3.1 After the promise becomes rejected, onrejected should be called, and the parameter is reason
    3.2 It should not be called until the promise becomes rejected
    3.3 Can only be executed once
  4. onfulfilled and onrejected should be microtasks
    queueMicrotask implements the invocation of microtasks
  5. then method can be called multiple times
    5.1 After promise becomes fulfilled, all onfulfilled callbacks should be executed in the order of then

    When implementing promises, we need an array to store the cb of onfulfilled
        promise.then(cb1).then(cb2).then(cb3)...
        // or
        const promise = new Promsie();
        promise.then(cb1);
        promise.then(cb2);
        // 

    5.2 After promise becomes rejected, all onrejected callbacks should be executed in the order of then

    When implementing promises, we need an array to store the onrejected cb
  6. return value
    The return value of then is a promise, new? old?

        promsie2 = promise1.then(onfulfilled, onrejected)

    6.1 The result of onfulfilled and onrejected execution is x, call resolvePromise
    6.2 Exception of onfulfilled and onrejected execution, promise2 needs to be rejected
    6.3 If onfulfilled is not a function, promise2 triggers fulfillment with the value of promise1
    6.4 If onrejected is not a function, promise2 triggers rejected with the reason of promise1

  7. resolvePromise

        resolvePromsie(promise2, x, resolve, reject);

    7.1 If promise2 and x are equal, then reject typeErrorWill die and cause an infinite loop, why? ? ?
    7.2 If x is a promise

    If x is pending, the state of promise must also be waiting/pending, until x becomes fulfilled/rejected
     If x is fulfilled, the transparent transmission of fulfilled promise width the same value
     if x is rejected, reject promise width the same reason

    7.3 If x is an object or a function

    let then = x.then;
     If x.then goes wrong, try catch(e), reject(e) **If it is an object, how can it go wrong? ? ? **
     If then is a function, then.call(x, resolvePromsieFn, rejectedPromiseFn);
    
     resolvePromsieFn input parameter y, execute resolvePromsie(promise2, y, resolve, reject);
     If an exception e is thrown when calling then, reject reason