# await spread operator (await...)

**URL:** <https://es.discourse.group/t/await-spread-operator-await/1251>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [March 12, 2022, 7:02pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251 "2022-03-12T19:02:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![tugrul](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tugrul/32/353_2.png) [@tugrul](https://es.discourse.group/u/tugrul)\
**Post date:** [March 12, 2022, 7:02pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251/1 "2022-03-12T19:02:04Z")

</div>

Similar fashion of spread operator `...`, but await each promises before expanding with the new operator `await...`

[Proposal link](https://github.com/tugrul/tc39-proposal-await-spread-operator)

```nohighlight
// const [foo, bar] = await Promise.all([taskFoo(), taskBar()]);
const [foo, bar] = [await...[taskFoo(), taskBar()]];

const obj = {await...{name: getAsyncName()}, await...{surname: getAsyncSurname()}};

otherFunc(await...[getAsyncName(), getAsyncSurname()])

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 12, 2022, 9:09pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251/2 "2022-03-12T21:09:14Z")

</div>

This seems like it overlaps with  
[https://github.com/tc39/proposal-await.ops](https://github.com/tc39/proposal-await.ops), and doesn’t provide for race/allSettled/any semantics.

This one, however, does provide a brand new combinatorial - on objects with Promise-valued properties - which seems like something that should have a Promise static method before syntax?

---

<div class="post-metadata">

**Author:** ![tugrul](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tugrul/32/353_2.png) [@tugrul](https://es.discourse.group/u/tugrul)\
**Post date:** [March 13, 2022, 1:11am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/3 "2022-03-13T01:11:06Z")

</div>

> [@ljharb](#):
>
> This seems like it overlaps with  
> [GitHub - tc39/proposal-await.ops: Introduce await.all / await.race / await.allSettled / await.any to simplify the usage of Promises](https://github.com/tc39/proposal-await.ops), and doesn’t provide for race/allSettled/any semantics.

Yes but not exact same thing. So `allSettled` can be included the spreading fashion `await!..` or something similar. `race` and `any` cannot handle by spread operator because they are conditional. They should choose only one item from the array of promises.

I have an idea for `race` and `any` apart from the spread idea.

```js

// const fastVal = await Promise.race(func1(), func2());
const fastVal = await ! (func1() | func2());
// or
const fastVal = await ! [func1() , func2()];

// const anyVal = await Promise.any(func1(), func2());
const anyVal = await ? (func1() | func2());
// or 
const anyVal = await ? [func1() , func2()];

```

> [@ljharb](#):
>
> - which seems like something that should have a Promise static method before syntax?

Can you explain a little bit? Do you mean async context?

Thanks,  
Tuğrul

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 13, 2022, 1:22am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/4 "2022-03-13T01:22:33Z")

</div>

No, I mean like `Promise.spread` or something, that can take an object with Promises as values - before adding syntax that can do that.

---

<div class="post-metadata">

**Author:** ![tugrul](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tugrul/32/353_2.png) [@tugrul](https://es.discourse.group/u/tugrul)\
**Post date:** [March 13, 2022, 1:33am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/5 "2022-03-13T01:33:11Z")

</div>

> [@ljharb](#):
>
> I mean like `Promise.spread` or something, that can take an object with Promises as values - before adding syntax that can do that.

I don't think `Promise.spread` static function is necessary. But let me see if you have an idea

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 13, 2022, 1:56am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/6 "2022-03-13T01:56:24Z")

</div>

I think that no syntax proposal should be combining promises in a way that isn't already possible with `.then` or Promise static methods, which includes objects with promise-valued properties.

---

<div class="post-metadata">

**Author:** ![tugrul](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tugrul/32/353_2.png) [@tugrul](https://es.discourse.group/u/tugrul)\
**Post date:** [March 13, 2022, 2:34am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/7 "2022-03-13T02:34:37Z")

</div>

Actually you can write for your own Promise static methods. Do they have to be? Also other functionalities...

Please make my previous message visible.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 13, 2022, 3:27am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/8 "2022-03-13T03:27:00Z")

</div>

I think we're talking past each other.

I'm saying that as a prerequisite for adding syntax that can await "an object with Promise values", there must first exist an API for it, just like `await`, and object spread.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [March 13, 2022, 3:48am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/9 "2022-03-13T03:48:09Z")

</div>

@ljharb's suggestion makes sense. EcmaScript generally has a high syntax bar, they don't like adding new syntax left and right. So one way to "proove" that a particular syntax request is useful enough is to implement that request as a new API and see how strongly the community adopts it.

So, in this scenario, we would add a Promise.spread(), or Promise.allMembers(), or whatever to resolve all of the promises within an object. If it turns out this feature is being used everywhere you look, then they might consider adding a syntactic version to make it easier to read.

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [March 13, 2022, 5:34am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/10 "2022-03-13T05:34:52Z")

</div>

For arrays / iterables, i think `await.all` is more clear, and even then I'm not convinced new syntax is justified given that `await Promise.all()` works just as well.

For object likes, I agree there should first be an API to do this before considering syntax. When we informally discussed this in the past, it wasn't obvious to me that a plain object is always what's wanted. I've actually used a more verbose but more versatile `Promise.allEntries`. [Promise.allEntries · GitHub](https://gist.github.com/mhofman/75ba4304ff4cd4bfac45327ca0ecbe3b)

---

<div class="post-metadata">

**Author:** ![tugrul](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tugrul/32/353_2.png) [@tugrul](https://es.discourse.group/u/tugrul)\
**Post date:** [March 13, 2022, 2:50pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251/11 "2022-03-13T14:50:05Z")

</div>

There is a little confusion. This feature is not target to replace Promise static methods.

The feature is `await` version of `...` and it is making `Promise.all` logic occasionally. Please consider in this perspective.

## sync version of spread

### stage 1

```js
const first = getFirstSync();
const second = getSecondSync();

const values = [...[first, second], ...[3, 4]];

```

### stage 2

```js
const values = [...[getFirstSync(), getSecondSync()], ...[3, 4]];

```

## async version of spread

### stage 1

```js
const first = getFirstAsync();
const second = getSecondAsync();

const values = [...[await first, await second], ...[3, 4]];

```

### stage 2

```js
const values = [await...[getFirstAsync(), getSecondAsync()], ...[3, 4]];

```

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [March 15, 2022, 4:41am UTC](https://es.discourse.group/t/await-spread-operator-await/1251/12 "2022-03-15T04:41:35Z")

</div>

```javascript
const values = [...await Promise.all([getFirstAsync(), getSecondAsync()]), ...[3, 4]];

```

```javascript
const values = [...await.all [getFirstAsync(), getSecondAsync()], ...[3, 4]];

```

When considering arrays only, I do not see any case where your `await...` cannot be substituted by `...await.all `

To be honest, I would probably write those avoiding spread altogether:

```javascript
const values = await Promise.all([getFirstAsync(), getSecondAsync(), 3, 4]);

```

```javascript
const values = await.all [getFirstAsync(), getSecondAsync(), 3, 4];

```

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [April 4, 2022, 11:29pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251/13 "2022-04-04T23:29:19Z")

</div>

Yeah, I'd say over 90% of my uses have been one of two things:

1. Starting a relatively small constant number of tasks concurrently and awaiting their results
2. Iterating an array of tasks and performing all of them maximally parallel without regard for their return value (though I've been doing this less and less lately as it tends to interfere with the throughput of other tasks)

---

<div class="post-metadata">

**Author:** ![jschoi](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jschoi/32/917_2.png) [@jschoi](https://es.discourse.group/u/jschoi)\
**Post date:** [May 9, 2022, 2:36pm UTC](https://es.discourse.group/t/await-spread-operator-await/1251/14 "2022-05-09T14:36:44Z")

</div>

For what it’s worth, there is the [Array.fromAsync proposal](https://github.com/tc39/proposal-array-from-async/) that would serve as the sequential version of Promise.all. I was imagining that it would set up the machinery for a future `await ...x` syntax that would be equivalent to `...await Array.fromAsync(x)`, although the latter would just be a syntactic nicety that perhaps avoids an unnecessary array allocation.
