Promises

Kevin Smith khs4473 at gmail.com
Wed Nov 7 06:58:20 PST 2012


> In other words, futures provide synchronisation, while promises
> provide resolution.
>

This is exactly the API that Q (and it's derivatives) use, although the
nomenclature is different.  In Q, the "future" is called a promise, and the
"promise" is what you get from calling defer():

    let { resolve, reject, promise } = Q.defer();

I think the nomenclature you've provided is superior.  Using those names
we'd have an API that looks something more like this:

    let promise = new Promise();
    let future = promise.future;

    // Futures have a then method, ala Promises/A+
    future.then(val => { ... });

    // Promises are resolved using methods on the promise object:
    promise.resolve(val);
    promise.reject(val);

The aesthetic issue I have with Promises/A+ is that the error handling
interface is ugly:

    future.then(val => {
      ...
    }, err => {
      ...
    });

In all of our APIs we try to avoid placing callbacks in non-terminating
argument positions, which this violates.

The only hard part that isn't really addressed by currently library
implementations is error handling.  I feel pretty strongly that rejections
(and by extension, errors thrown from `then` callbacks), should ALWAYS be
observable.  In other words, if a rejection is not observable via error
listeners at the time when error listeners should be called, then an
unhandled error should be thrown by the runtime.

In my usage of promises I have never wanted anything other than this
behavior.

- Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121107/a63c69f4/attachment.html>


More information about the es-discuss mailing list