The purpose of `await` is to allow synchronous execution within an
async function.

The purpose of `then` is to make it easier to manipulate the promises
as if they were normal values. It is much closer to
`Promise.prototype.then` than to `await`, since it does not prevent
the statements following it from execution. For this reason it doesn't
need to be in an async function.

The synchronous nature of `await` can be annoying when you have
multiple promise-creating expressions that you want to run in
parallel. You have to run the expressions first, keep track of the
promises they evaluate to, and then use `await` on those promises.
`Promise.all` helps in this endeavor, but it prevents you from putting
expressions exactly where you want them to be.

Consider the following:
```js
const pets = (await getDogs()).concat(await getCats())
```
This would not be ideal if you want `getDogs` and `getCats` to start
off in parallel. So you need to do something like this:
```js
const [dogs, cats] = await Promise.all([getDogs(), getCats()])
const pets = dogs.concat(cats)
```
With `then` it could be done like so:
```js
const pets = await (then getDogs()).concat(then getCats())
```

On Sat, Mar 3, 2018 at 12:12 AM, Peter Jaszkowiak <p.jasz...@gmail.com> wrote:
> Why not just use async/await then? Seems like if you were to replace `then`
> with `await` in your top example it would work exactly as you want (as long
> as it's in an async function).
>
> On Sat, Mar 3, 2018 at 1:06 AM, Bryant Petersen <petersen...@gmail.com>
> wrote:
>>
>> I've put the details here:
>> https://github.com/bwpetersen/proposal-then-operator
>>
>> The basic idea is that the `then` operator would allow you to
>> manipulate a promise as if it were its resolved value. Expressions
>> containing `then` become similar to an `onFulfillment` handler.
>>
>> Here is an example:
>>
>> ```js
>> const greeting = `Hello, ${(then getUser()).name}. You have ${(then
>> getMessages()).length} messages.`
>> // This would behave like:
>> const greeting = async () => {
>>   const user = getUser()
>>   const messages = getMessages()
>>   return `Hello, ${(await user).name}. You have ${(await
>> messages).length} messages.`
>> }()
>> ```
>>
>> I'm looking for feedback on this idea.
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to