Awaiting block expression
const image = await [null].reduce(async user => fetch('/image/'+ (await user).image).json(), fetch('/user/'+id).json())
or
const image = await fetch('/image/'+ (await (await fetch('/user/'+id)).json()).image).json()
Or use Promise.all()
. Or an immediately invoked async arrow function
where a value is return
ed from the function.
How do you propose to get the values declared within block scope (a different scope) outside of the block?
How does JavaScript know const image = {}
is intended to be block scope and not a JavaScript plain object declaration?
Actually... You are proposing a await
in a block, and a implicit
await in all Promile-like expressions (or every expression) in the
block, that's a totally different beast
I don't think the implicit await part can be easily implemented neither is a good idea, the code flow can get really confusing, imagine the following:
const foo = await {
const bar = {
then(callback) {
},
};
return bar;
}
What should be the result? Translating your proposal to already valid code it should be:
const foo = await (async function() {
const bar = await {
then(callback) {
},
};
return bar;
}());
Foo will never be resolved, because the awaited bar
object never
calls the callback argument, and the whole block stagnates waiting for
something that will never happen
Em sex, 21 de jun de 2019 às 10:40, Augusto Moura <augusto.borgesm at gmail.com> escreveu:
How about like do expression:
const image = await (async do {
const user = await fetch('/user/'+id).json();
await fetch('/image/'+user.image).json()
})
As there are more and more async apis, i would like to have a block, where all promises within automaticly are awaited:
Proposal: