Awaiting block expression

# Tobias Buschor (6 years ago)

As there are more and more async apis, i would like to have a block, where all promises within automaticly are awaited:

Proposal:

const image = await {  
    const user = fetch('/user/'+id).json();  
    fetch('/image/'+user.image).json();  
}  
# Augusto Moura (6 years ago)

The do expressions proposal1 had some discussions about having a async variant2 you should give it a look Is the same concept you are proposing

Em sex, 21 de jun de 2019 às 03:42, Tobias Buschor <tobias.buschor at shwups.ch> escreveu:

# guest271314 (6 years ago)

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 returned 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?

# Augusto Moura (6 years ago)

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:

# Colin Cheng (6 years ago)

How about like do expression:

const image = await (async do {
   	const user = await fetch('/user/'+id).json();
    await fetch('/image/'+user.image).json()
})