Promises/B
From CommonJS Spec Wiki
< Promises
STATUS: PROPOSAL
- Implementations
- [[Implementations/Narwhal|]] (0.5), [[Implementations/node.js|]] (http://github.com/kriskowal/q)
Specification
- How to send messages to a promise object is beyond the scope of this specification.
- How to detect whether a value is a promise object is beyond the scope of this specification.
- These behaviors are an exercise left to implementors.
- Users of promises must not depend on any particular implementation's behavior.
(See Promises/D for a specification that illuminates all of the above details, or Promises/A for a syntactically convenient subset of this API)
The "promise" (provisionally "promise-b") module exports the following API:
- when(value, callback, errback_opt)
- Arranges for callback to be called:
- with the fully resolved value of the given value
- in a future turn of the event loop
- if and when the value is or becomes a fully resolved value accessible to the caller.
- Arranges for errback to be called:
- with a value representing the reason why the value will never be fully resolved
- in a future turn of the event loop
- if the value is a promise and
- if that promise is resolved with a rejection ("reject").
- Returns a promise that may be eventually resolved with the value returned by either the callback or the errback.
- The value may be any value.
- If the value is a promise, sends a "when" message to that promise with a callback and an errback that intercept the resolution of that promise and resolve the promise returned by "when":
- with the value received by the callback
- or with a rejection ("reject") with the reason given to the errback.
- Callback must not be called before when returns.
- Errback must not be called before when returns.
- Callback must only be called once.
- Errback must only be called once.
- If callback is called, errback must never be called.
- If errback is called, callback must never be called.
- "when" does not guarantee that callback or errback will ever be called.
- asap(value, callback, errback_opt)
- Arranges for callback to be called with the fully resolved value of the given value when the value is or becomes a fully resolved value accessible to the caller.
- The value may be any value.
- If the value is a promise, "asap" sends a "when" message to that promise with a callback and an errback that intercept the resolution of that promise and resolve the promise returned by "asap":
- with the value given to the callback
- or with a rejection ("reject") with the reason given to the errback.
- If the value is not a promise, "asap" must call the callback immediately.
- If the callback does not return a promise, "asap" returns the value returned by "callback".
- If the callback returns a promise, "asap" sends a a "when" message to that promise with a callback and errback that intercept the resolution of that promise and resolve the promise returned by "asap":
- with the value given to the callback
- or with a rejection ("reject") with the reason given to the errback.
- "asap" does not guarantee that callback or errback will ever be called.
- Callback must only be called once.
- Errback must only be called once.
- If callback is called, errback must never be called.
- If errback is called, callback must never be called.
- Callback and errback may never be called.
- enqueue(task Function)
- causes a function to be called as soon as possible in a future turn.
- get(object, name)
- Returns a promise for a property of the given object.
- post(object, name, args)
- Returns a promise for the return value of calling a member function of the given object with the given arguments.
-
args
are not variadic.
- put(object, name, value)
- returns a promise to set the value of a named property of the object and to forward the value when that has been completed.
- del(object, name)
- Returns a promise to delete the named property of the object and to forward
undefined
when that has been completed.
- makePromise(descriptor Object, fallback Function)
- Returns a Promise object
- Must be callable as a function
- May be instantiable as a constructor
- Accepts a descriptor Object that maps message names to handler functions, particularly:
- when(errback)
- get(name)
- put(name, value)
- post(name, args)
- del(name)
- Each of the above handler functions must return a more resolved value or promise.
- Accepts a fallback(message, ...args) function that receives all messages that are not handled by the promise descriptor and must return a more resolved value or promise.
- Returns a promise.
- defer()
- Returns an Object with a "resolve(value)" property function, and a "promise" property.
- The first time that "resolve(value)" is called, the state of the "promise" advances to "resolved" and all previous and future promises observing the "promise" are resolved.
- If the "value" is not a promise, the resolved value of the "promise" becomes a reference ("ref") to the given value.
- All subsequent calls to resolve must be silently ignored.
- reject(reason String)
- Returns a rejected promise with the given reason.
- A rejected promise handles the "when(errback)" message in one of two ways:
- If an errback is provided, the errback is called with the reason as its argument, and the "when" message handler must return the value returned by the errback.
- If no errback is provided, the "when" message handler must return a new rejection ("reject") with the same reason.
- Rejects all other messages.
- ref(value)
- If value is a promise, returns that promise.
- If the value is not a promise, returns a resolved promise that handles the following messages:
- when(errback) ignores the errback and forwards the resolved value.
- get(name) forwards the named property of the resolved value.
- put(name, value) sets the named property to the given value of the resolved value and forwards the value.
- del(name) deletes the named property of the resolved value and forwards
undefined
- post(name, args) calls the named method of the value and forwards the returned value.
- all other messages forward a rejection ("reject") with the reason "Promise does not handle NAME" where NAME is the name of the message.
- isPromise(value) Boolean
- returns whether the given value is a promise.
- method(name String)
- returns a function like "get", "put", "del", and "post" except with the given name that must forward a message to a promise in a future turn.