[whatwg] Promise.every() arguments
Alec Flett
alecflett at chromium.org
Tue Aug 20 13:42:43 PDT 2013
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
More information about the whatwg
mailing list