promises-aplus / synchronous-inspection-spec Goto Github PK
View Code? Open in Web Editor NEWAn extension to the Promises/A+ spec for synchronously retrieving fulfillment values and rejection reasons
An extension to the Promises/A+ spec for synchronously retrieving fulfillment values and rejection reasons
This proposal extends the Promises/A+ specification to cover synchronous inspection of a promise's fulfillment value or rejection reason.
It is not expected that all Promises/A+ implementations will include this extension. If the features of this extension are desired, you should test for them:
if (typeof promise.inspect === "function") {
// Use the `inspect` method, assuming it conforms to the contract below.
}
TODO
inspect
methodA promise implementing this specification must have an inspect
method, which returns an object.
inspect
fulfillmentValue
.rejectionReason
.inspect
fulfillmentValue
, whose value is equal to the promise's fulfillment value.rejectionReason
.inspect
fulfillmentValue
.rejectionReason
, whose value is equal to the promise's rejection reason.Since fulfillment values and rejection reasons can be any valid JavaScript value, including undefined
, this specification hinges on the difference between a property being present and it being absent. That is, the proper way to synchronously test for a promise being fulfilled is not if (promise.inspect().fulfillmentValue)
or even if (promise.inspect().fulfillmentValue !== undefined)
, but instead if ("fulfillmentValue" in promise.inspect())
.
I was about to commit this for discussion, before I realized a potentially fatal flaw that made me contemplate Draft B (#2).
This proposal extends the Promises/A+ specification to cover synchronous inspection of a promise's fulfillment value or rejection reason.
It is not expected that all Promises/A+ implementors will implement this extension. If the features of this extension are desired, you should test for them as in the "Branding" section below.
TODO
An implementation of the synchronous inspection extension must signal that synchronous inspection is available by adding a truthy synchronousInspection
property to every then
method:
if (promise.then.synchronousInspection) {
// Proceed to use the properties described below.
}
fulfillmentValue
.rejectionReason
.fulfillmentValue
, whose value is equal to the promise's fulfillment value.rejectionReason
.fulfillmentValue
.rejectionReason
, whose value is equal to the promise's rejection reason.fulfillmentValue
or rejectionReason
, this must not have any impact on the promise's state.If feasible, an implementation is encouraged to make fulfillmentValue
and rejectionReason
properties non-writable, to discourage attempts to set them.
Since fulfillment values and rejection reasons can be any valid JavaScript value, including undefined
, this specification hinges on the difference between a property being present and it being absent. That is, the proper way to synchronously test for a promise being fulfilled is not if (promise.fulfillmentValue)
or even if (promise.fulfillmentValue !== undefined)
, but instead if ("fulfillmentValue" in promise)
.
I've left the motivation sections as TODOs.
I think the templating use case, as explained by @ForbesLindesay here, is a good one.
I also think that assimilation could benefit. Currently, you can only assimilate by waiting a turn. Thus, there are usually at least two turns in consuming an assimilated promise: foreignPromise.then(deferred.resolve, deferred.reject)
and nativePromise.then(...)
. With synchronous inspection available, the first step could be made synchronous while not losing the guaranteed asynchronicity of the second.
This proposal extends the Promises/A+ specification to cover synchronous inspection of a promise's fulfillment value or rejection reason.
It is not expected that all Promises/A+ implementations will include this extension. If the features of this extension are desired, you should test for them:
if (typeof promise.asap === 'function') {
// Use the `asap` method, assuming it conforms to the contract below.
}
TODO
asap
methodA promise implementing this specification must have an asap
method.
A promises asap
method accepts two arguments
promise.asap(onFulfilled, onRejected)
promise.asap
must behave in the same way as then
as described in http://promises-aplus.github.com/promises-spec/#the__method But for the purposes of asap
3.2.4
and Notes 4.1
can be ignored as asap is permitted to call onFulfilled
and onRejected
synchronously.
This deliberately makes the API for synchronous inspection similar to that of asynchronous inspection to discourage anti-patterns like polling for completion. It is also optimized for the common use case of a promise that may or may not be completed already.
This proposal extends the Promises/A+ specification to cover synchronous inspection of a promise's fulfillment value or rejection reason.
It is not expected that all Promises/A+ implementations will include this extension. If the features of this extension are desired, you should test for them:
if (typeof promise.asap === 'function') {
// Use the `asap` method, assuming it conforms to the contract below.
}
TODO
asap
methodA promise's asap
method accepts two arguments:
promise.asap(onFulfilled, onRejected)
onFulfilled
and onRejected
are optional arguments:
onFulfilled
is not a function, it must be ignored.onRejected
is not a function, it must be ignored.onFulfilled
is a function:
promise
is fulfilled, with promise
's fulfillment value as its first argument.onRejected
has been called.onRejected
is a function,
promise
is rejected, with promise
's rejection reason as its first argument.onFulfilled
has been called.asap
may be called multiple times on the same promise.
promise
is fulfilled, respective onFulfilled
callbacks must execute in the order of their originating calls to asap
.promise
is rejected, respective onRejected
callbacks must execute in the order of their originating calls to asap
.asap
returns undefined
and allows errors to be thrown synchronously.This deliberately makes the API for synchronous inspection similar to that of asynchronous inspection to discourage anti-patterns like polling for completion. It is also optimized for the common use case of a promise that may or may not be completed already.
This proposal extends the Promises/A+ specification to cover synchronous inspection of a promise's fulfillment value or rejection reason.
It is not expected that all Promises/A+ implementations will include this extension. If the features of this extension are desired, you should test for them:
if (typeof promise.inspectState === "function") {
// Use the `inspectState` method, assuming it conforms to the contract below.
}
TODO
inspectState
methodA promise implementing this specification must have an inspectState method
, which returns an object.
isFulfilled
with a value of false
isRejected
with a value of false
fulfillmentValue
rejectionReason
isFulfilled
with a value of true
isRejected
with a value of false
rejectionReason
isFulfilled
with a value of false
isRejected
with a value of true
fulfillmentValue
undefined
, you must check isFulfilled
and isRejected
. That is, the proper way to synchronously test for a promise being fulfilled is not if (promise.inspectState().fulfillmentValue)
or even if (promise.inspectState().fulfillmentValue !== undefined)
, but instead if (promise.inspectState().isFulfilled)
.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.