Promises

Kevin Smith khs4473 at gmail.com
Wed Nov 14 08:23:07 PST 2012


> If the second argument is optional, it's possible to have both one-arg and
> two-arg styles in the same API.
> What do people think about this idea?
>

Maybe - minimalism served the class proposal quite well.  It might be a
good strategy here, too.

Here's what I'm thinking:

    // Creates a new promise
    let promise = new Promise();

    // Resolves the promise (ala Q)
    promise.resolve(value);

    // Rejects the promise (ala Q)
    promise.reject(value);

    // A handle to the eventual value of the promise
    promise.future;

    // The then method (ala Promises/A+)
    promise.future.then(val => {

        // Success handler

    }, err => {

        // Error handler
    });

    // Returns a future for the value
    Promise.when(value);

    // Returns a rejected future with the specified error
    Promise.reject(error);

    // Returns a future for every eventual value in the list
    Promise.whenAll(list);

    // Returns a future for the first resolved future in the list
    Promise.whenAny(list);

Initial implementation here: https://github.com/jscloud/Promise

I think it's important to separate "Promise" from "Future".  Back in the
CommonJS mailing list days, there was contention between Promises/A
(thenables) and Promises/B (basically Q).  But they really are
complementary:  futures and promises.

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


More information about the es-discuss mailing list