promises-aplus / synchronous-inspection-spec

An extension to the Promises/A+ spec for synchronously retrieving fulfillment values and rejection reasons
5 stars 1 forks source link

Draft C #4

Closed ForbesLindesay closed 11 years ago

ForbesLindesay commented 11 years ago

Promieses/A+ Extension: Synchronous Inspection

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.
}

Motivation

TODO

Requirements: the asap method

A 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.

Note

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.

ForbesLindesay commented 11 years ago

Implementations can of course provide suggar:

Q.isPending = function (promise) {
  var isPending = true;
  promise.asap(function () { isPending = false; },
               function () { isPending = false; });
  return isPending;
};

Q.isFulfilled = function (promise) {
  var isFulfilled = false;
  promise.asap(function () { isFulfilled = true; });
  return isFulfilled;
};

Q.isRejected = function (promise) {
  var isRejected = false;
  promise.asap(undefined, function () { isRejected = true; });
  return isRejected;
};
domenic commented 11 years ago

asap in any form has been vetoed by at least two members of the Promises/A+ organization, as well as MarkM (whose opinion, as part of TC39, should not be discounted). Closing to prevent confusion from those thinking this is actively being considered for standardization under the Promises/A+ banner, but of course discussion is still welcome.

domenic commented 11 years ago

Argh wait I can't close this O_o