Modify Promise.all() to accept an Object as a parameter

bread at mailed.me.uk bread at mailed.me.uk
Fri Apr 10 08:44:52 UTC 2015


This proposal came out of https://github.com/tc39/ecmascript-asyncawait/issues/25#issuecomment-91356442

The proposal is to augment Promise.all() to accept an Object as a parameter, which return a corresponding object (or Promise for such an object) such the each field of the Object, if it were a Promise is resolved, and if not it is otherwise returned unmodified.

The exact semantics are such that each data member of the Object is treated as if it were an Array entry as passed to Promise.all() currently.

The purpose of the modification is to allow labelling of each Promise at a point close to the value of the Promise to quickly and easily show the developers intent.

Implementation is relatively easy, and Promise implementation agnostic (although the simplicity of the API makes implementation-specific optimization possible).

No view has been taken on how non-Promise/value members of the Object should be treated (ignored? throw Error?), or whether nested Objects should be resolved (potentially confusing, but useful for combining results from other calls that return Objects containing Promises for final resolution).

For example (taken from https://github.com/tc39/ecmascript-asyncawait/issues/25):

var r = await Promise.all({
URL:getUrl("home"),
input:doUserInput(elem)
}) ;

redirect(r.URL+"?"+r.input);
...as opposed to:
var r = await Promise.all([
getUrl("home"),
doUserInput(elem)
]) ;

redirect(r[0]+"?"+r[1]);
This has the advantage of keeping the operation and name in close proximity, making the intent easier to spot, is fully ES5 friendly with respect to the await expression and response, allows for nested parallel Promises, and in any case is a runtime implementation and so the programmer can alias the function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150410/8b6125e0/attachment-0001.html>


More information about the es-discuss mailing list