Promises provide a well-defined interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time. By utilizing a standard interface, different components can return promises for asynchronous actions and consumers can utilize the promises in a predictable manner. Promises can also provide the fundamental entity to be leveraged for more syntactically convenient language-level extensions that assist with asynchronicity.
-
Promise()
Module exports
Promise
function. All the promises MUST derive fromPromise.prototype
. ExportedPromise
can be used to identifyPromise
objects:object.valueOf() instanceof Promise
Specification recognizes three states of the
Promise
instances:-
pending
Instance of
Promise
is pending if following evaluates totrue
:promise.valueOf().valueOf() === promise.valueOf()
-
fulfilled
Instance of
Promise
is fulfilled if following evaluates totrue
:!(promise.valueOf().valueOf() instanceof require('promise').Promise)
-
broken
Instance of
Promise
is broken if following evaluates to true:promise.valueOf() instanceof require('promise').Broken
The promise may only move from pending to fulfilled or from pending to broken and only once. Regardless whether promise was fulfilled or broken it's value MUST not be changed, just as a values in JavaScript, primitives and object identities, can not change (although objects themselves may always be mutable even if there identity isn't).
-
-
Promise.make()
Returns an object containing a new promise:
-
promise
Property is instance of
Promise
and represents newly created promise in pending state. -
emit(String(event), args...)
Function emits events for a listeners of the bound promise. Events MAY be emitted to transition bound promise from one state into another. Emitting events that are not recognized by this spec is absolutely legal while promise is in pending state, in fact it is expected that events like
"progress"
may be emitted by promises that representforEach
-able streams.If
emit
is called for a pending promise:- With
"fulfill"
first argument & instance ofBroken
second argument promise will transition to a broken state. Emitting any events for this promise will have no effect, neither listeners will be notified. - With
"fulfill"
first argument & instance ofPromise
second argument promise will stay in pending state and will be automatically fulfilled with a value of a promise that was passed as a second argument, once it will be fulfilled. Meanwhile all the events emitted for a fulfilling promise will be forwarded to listeners of this one. - With
"fulfill"
first argument & anything other then instance ofBroken
and instance ofPromise
second promise will transition to fulfilled state. Emitting any events for this promise will have no effect, neither listeners will be notified. - With
"brake"
first argument and anything for a second argument, then instance ofBroken
is created with a second argument and promise is fulfilled with it, that will transitions promise to the broken state. Emitting any events for this promise will have no effect, neither listeners will be notified.
When events are emitted for promise all their registered listeners for that event are are called in the next turn of event loop with a promise first argument and all the arguments passed to the emit function starting from second as following ones.
- With
-
-
Broken(object)
Module exports
Broken
class that derives formError
and may be used to identify promises that got broken. Instances may be created by calling this function with or withoutnew
keyword. If instance ofBroken
is passed to the constructor it will be immediately returned. If not all the properties of the object will be copied to the instance. Constructor also accepts strings that will be exposed as a message property of returned instance. -
on(promise, String(event), listener)
Exported function is used to register event listeners to the
specified event
on for the specified promise
. Non Promise
objects or promises that are in non pending state will be
ignored.
Example of thenable promises on top of this.