W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2013

[whatwg] Promise.every() arguments

From: Alec Flett <alecflett@chromium.org>
Date: Tue, 20 Aug 2013 13:42:43 -0700
Message-ID: <CAHWpXeaLFzRixj5NfVnKuWOg+aSVdb3gvMqHG52PfBZGHF_Ppw@mail.gmail.com>
To: whatwg@lists.whatwg.org
I've been doing a lot of experimentation with Promises using the Blink
implementation. I've frequently hit an issue with the .every() / .any() /
.some()

the problem is that they support a variable number of arguments. This seems
very developer friendly in theory, as per the docs:

Promise.every(fetchJSON(foo), fetchJSON(bar), fetchJSON(baz));

This is great the first time you play with it on your local developer
console.

The problem arises in practice: it's very common to build up arrays of N
promises, and then tie them all together. Even if your own API uses
varargs, using Promise.every breaks down.

function getDocuments(requests) {
    var pending = [];
    for (var i = 0; i < requests.length; ++i) {
        var url = extractUrl(requests[i]);
        pending.push(fetchJSON(url));
    };

    // nope, this won't work!
    // return Promise.every(pending);

    // only this works
    Promise.every.apply(Promise, pending);
}

this is how (and why) kiskowal's Q works with an array as the single
parameter:

https://github.com/kriskowal/q#combination

Alec
Received on Tuesday, 20 August 2013 20:51:05 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:09:23 UTC