Giter Club home page Giter Club logo

promises-spec's Introduction

Promises/A+

This proposal clarifies the behavioral clauses of the Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.

As with Promises/A, this proposal does not deal with how to create, fulfill, or reject promises.

For a full description of the differences between Promises/A+ and Promises/A, see Differences from Promises/A.

General

A promise represents a value that may not be available yet. The primary method for interacting with a promise is its then method.

Terminology

  1. "promise" is an object or function that defines a then method.
  2. "value" is any legal JS value, including undefined, that is not a promise.
  3. "reason" is a value that indicates why a promise was rejected.
  4. "must not change" means immutable identity (i.e. ===), but does not imply deep immutability.

Requirements

Promise States

  1. A promise must be in one of three states: pending, fulfilled, or rejected.
  2. When in pending:
    1. a promise may transition to either the fulfilled or rejected state.
    2. if the promise transitions to fulfilled, it must provide you a way to call a function with the promise's fulfillment value.
    3. if the promise transitions to rejected, it must provide you a way to call a function with the promise's reason value.
  3. When in fulfilled:
    1. a promise must not transition to any other state.
    2. a promise must have a value, which must not change.
    3. a promise must provide you a way to call a function with the promise's fulfillment value.
  4. When in rejected:
    1. must not transition to any other state.
    2. a promise must have a reason, which must not change.
    3. a promise must provide you a way to call a function with the promise's reason value.

The then Method

A promise's then method accepts the following two arguments:

promise.then(onFulfilled, onRejected)
  1. Both onFulfilled and onRejected are optional arguments:
    1. If onFulfilled is not a function, it must be ignored.
    2. If onRejected is not a function, it must be ignored.
  2. If onFulfilled is a function:
    1. it must be called after promise is fulfilled, with promise's fulfillment value as its first argument.
    2. it must not be called more than once.
    3. it must not be called if onRejected has been called.
  3. If onRejected is a function,
    1. it must be called after promise is rejected, with promise's rejection reason as its first argument.
    2. it must not be called more than once.
    3. it must not be called if onFulfilled has been called.
  4. onFulfilled and onRejected must not be called before then returns [1].
  5. Calling then must return a promise [2]
  6. You may call then multiple times.
    1. If you supply onFulfilled or onRejected to a call to then, these callbacks must execute before any onFulfilled or onRejected functions supplied to subsequent then calls.

Promise Chaining

var promise2 = promise1.then(onFulfilled, onRejected);
  1. If either onFulfilled or onRejected returns a value, promise2 must be fulfilled with that value.
  2. If either onFulfilled or onRejected throws an exception, promise2 must be rejected with the thrown exception as the reason.
  3. If either onFulfilled or onRejected returns a promise (call it returnedPromise), promise2 must be placed into the same state as returnedPromise:
    1. If returnedPromise is pending, promise2 must remain pending until returnedPromise is fulfilled or rejected.
    2. If returnedPromise is fulfilled, promise2 must be fulfilled with the same value.
    3. If returnedPromise is rejected, promise2 must be rejected with the same reason.
  4. If onFulfilled is not a function and promise1 is fufilled, promise2 must be fulfilled with the same value.
  5. If onRejected is not a function and promise1 is rejected, promise2 must be rejected with the same reason.

Notes

  1. In practical terms, an implementation must use a mechanism such as setTimeout, or faster alternatives such as setImmediate or process.nextTick, to ensure that the JS engine does not invoke onFulfilled and onRejected in the same turn of the event loop as their parent then call.

  2. Implementations are free to allow promise2 === promise1, provided the implementation meets all requirements. Each implementation should document whether it can produce promise2 === promise1 and under what conditions.


CC0
To the extent possible under law, the Promises/A+ organization has waived all copyright and related or neighboring rights to Promises/A+ Promise Specification. This work is published from: United States.

promises-spec's People

Contributors

briancavalier avatar domenic avatar evangoer avatar forbeslindesay avatar lsmith avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.