switer / switer.github.io

Personal homepage
https://switer.github.io
5 stars 0 forks source link

Promise/A+ #17

Open switer opened 10 years ago

switer commented 10 years ago

Terminology

  1. “promise” is an object or function with a then method whose behavior conforms to this specification.
  2. “thenable” is an object or function that defines a then method.
  3. “value” is any legal JavaScript value (including undefined, a thenable, or a promise).
  4. “exception” is a value that is thrown using the throw statement.
  5. “reason” is a value that indicates why a promise was rejected.

    Requirements

    States

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

state behavior
pending may transition to either the fulfilled or rejected state.
fulfilled a. must not transition to any other state.
b. must have a value, which must not change.
rejected a. must not transition to any other state.
b. must have a reason, which must not change.

The then Method

A promise must provide a then method to access its current or eventual value or reason.

A promise’s then method accepts two arguments:

promise.then(onFulfilled, onRejected)

onFulfilled a. is optional arguments b. if is not a function, it must be ignored. c. it must be called after promise is fulfilled, with promise’s value as its first argument. d. it must not be called before promise is fulfilled. e. it must not be called more than once. onRejected a. is optional arguments b. if is not a function, it must be ignored. c. it must be called after promise is rejected, with promise’s reason as its first argument. d. it must not be called before promise is rejected. e. it must not be called more than once. then a. If/when promise is fulfilled/rejected, all respective onFulfilled/onRejected callbacks must execute in the order of their originating calls to then. b. must return a promise js promise2 = promise1.then(onFulfilled, onRejected); c. If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure

[${Resolve}](promise2, x).