5 ms·
There should also be something like awaitAll to replace await Promise.all calls.
by ujal 9y ago
There should also be something like
awaitAll
to replace
await Promise.all
calls.
- steve_adams_86 9y agoYou can make it yourself fairly easily I guess: const awaitAll = Promise.all.bind(Promise); async function foo() { var [val1, val2] = await awaitAll([func1, func1]); } It isn't ideal but it's a little cleaner
- nailer 9y ago+1. I'm a big fan of top level function binds to remove boilerplate and keep code readable: const log = console.log.bind(console), query = document.querySelector.bind(document), queryAll = document.querySelectorAll.bind(document);
- RReverser 9y agoYou don't need to bind `console.log` by the way (at least not in any recent version of Node.js or browsers).
- ujal 9y agoawait all([fn1, fn2]) ;) i wonder when we will get s-expressions for function calls (awaitAll [fn1, fn2])
- doublerebel 9y agoJust write (iced) coffeescript, s-exp are totally valid there: https://github.com/nextorigin/riemann-query-parser/blob/master/src/riemann-query.coffee https://github.com/nextorigin/riemann-query-parser/blob/mast...
- ujal 9y agoYeah, s-exps are the only thing I miss :(
- adrusi 9y agoThere are many ways to compose promises, no point in elevating some of them to keyword status. If you need to do anything interesting with promises, just use a promises library. The point of async/await is to free you from having to use callbacks, not to eliminate the need for libraries.
- ujal 9y agoEvery async function will return a Promise anyway. I am simply interested in resolving a collection of Promises in a clean way.
- abritinthebay 9y agoGiven Promise.all resolves to an ordered array surely this would work: const [foo, bar, baz] = await returnerOfPromiseCollection(); That would assume there was either a) a catch in that collect or b) some error checking, but still...
- bfred_it 9y agoIt "works" but foo won't contain the resolved value, just the promise. The code is awaiting an array, which is already "resolved" and its returned right away. Its contents are not relevant to `await`.
- abritinthebay 9y agoYou are correct! My apologies = I misremembered how that works >_< Yes, once you have a promise you have to deal with it as such through the whole chain.
- always_good 9y agoI don't see what's unclean about Promise.all or how creating a 1:1 alias for it makes it cleaner. Also, many times you want to limit the concurrency of your promise execution which isn't something you can do with an array of promises. You'd be back to using `await` + something like https://www.npmjs.com/package/promise.map https://www.npmjs.com/package/promise.map. I'm someone that used to use `co` where you could go: const [a, b] = yield [promiseA(), promiseB()] But I prefer the simplicity and consistency of having to use something like Promise.all or require('promise.map').
- Ajedi32 9y agoPretty sure there used to be a syntax in the spec for handling that. Something like `await*` I believe. It got removed pretty early on in the process though.
- ujal 9y agoAny interesting reasons as to why?
- Ajedi32 9y agoI wasn't really involved in those discussions, but I suspect it's for similar reasons to what others in the comments here are arguing; they wanted to keep the syntax minimal and decided that adding a special syntactic sugar for something that could already be accomplished by `Promise.all(...)` was unnecessary.
- krisdol 9y agoYou can use .map to call await on every element in the array, or define a new function so you don't have to write that all the time: const awaitAll = (futures) => futures.map(f => await f); edit: this is wrong but I can't delete it. Haven't used async as much as promises.