[whatwg] Promise.every() arguments

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