Re: Re: Modify Promise.all() to accept an Object as a parameter

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Re: Modify Promise.all() to accept an Object as a parameter

Adam Eisenreich
Back when async/await was introduced I struggeled quite a bit with promises arrays that have conditional promises. RxJS is moving from array only support in theirs operators to objects too, there seems to be an actual trend going on. Is there any reason against?

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Modify Promise.all() to accept an Object as a parameter

Jordan Harband
The current API accepts an *iterable*, which means any object that has `Symbol.iterator`, such as an array or a Set.

Throwing when it receives a non-iterable object is an important tool to catch bugs. If Promise.all was made to accept a non-iterable object as well, I suspect many bugs would go uncaught.

On Fri, Oct 11, 2019 at 10:15 AM Adam Eisenreich <[hidden email]> wrote:
Back when async/await was introduced I struggeled quite a bit with promises arrays that have conditional promises. RxJS is moving from array only support in theirs operators to objects too, there seems to be an actual trend going on. Is there any reason against?
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Modify Promise.all() to accept an Object as a parameter

Bradford Smith
Promise.all(Object.values(myObjWithPromiseValues)).then(...)

On Fri, Oct 11, 2019 at 10:22 AM Jordan Harband <[hidden email]> wrote:
The current API accepts an *iterable*, which means any object that has `Symbol.iterator`, such as an array or a Set.

Throwing when it receives a non-iterable object is an important tool to catch bugs. If Promise.all was made to accept a non-iterable object as well, I suspect many bugs would go uncaught.

On Fri, Oct 11, 2019 at 10:15 AM Adam Eisenreich <[hidden email]> wrote:
Back when async/await was introduced I struggeled quite a bit with promises arrays that have conditional promises. RxJS is moving from array only support in theirs operators to objects too, there seems to be an actual trend going on. Is there any reason against?
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Modify Promise.all() to accept an Object as a parameter

Jacob Bloom
What about special handling for Maps? Maybe something like

```
const requests = new Map();
requests.set('reqA', fetch('...'));
requests.set('reqB', fetch('...'));
const responses = await Promise.all(requests);
console.log(
  responses.get('reqA'),
  responses.get('reqB'),
);
```

...which would continue to fail for most objects and would encourage
using Map over POJOs for map-like data structures. (Since Maps are
iterable, this may break some existing code but I doubt it)

On Fri, Oct 11, 2019 at 11:28 AM Bradford Smith
<[hidden email]> wrote:

>
> Promise.all(Object.values(myObjWithPromiseValues)).then(...)
>
> On Fri, Oct 11, 2019 at 10:22 AM Jordan Harband <[hidden email]> wrote:
>>
>> The current API accepts an *iterable*, which means any object that has `Symbol.iterator`, such as an array or a Set.
>>
>> Throwing when it receives a non-iterable object is an important tool to catch bugs. If Promise.all was made to accept a non-iterable object as well, I suspect many bugs would go uncaught.
>>
>> On Fri, Oct 11, 2019 at 10:15 AM Adam Eisenreich <[hidden email]> wrote:
>>>
>>> Back when async/await was introduced I struggeled quite a bit with promises arrays that have conditional promises. RxJS is moving from array only support in theirs operators to objects too, there seems to be an actual trend going on. Is there any reason against?
>>> _______________________________________________
>>> es-discuss mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/es-discuss
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Boris Zbarsky
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Cyril Auburtin
OP probably means he would like Promise.all to return an object as well, if an object if given

It's possible to hack an object to be iterable, but Promise.all already return an array unfortunately

On Sun, Oct 13, 2019 at 7:16 PM Boris Zbarsky <[hidden email]> wrote:
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Isiah Meadows-2
Maybe Promise.join(object)? Also, if a map is passed (or any iterable), it should be joined into a map.

At the most basic level:

```js
Promise.join = (o) => {
    let isMap = o[Symbol.iterator] != null
    let ps = (
        isMap ? Array.from : Object.entries
    )(o)
    let ks = ps.map(p => p[0])
    return Promise.all(ps.map(p => p[1]))
    .then(vs => {
        let ps = vs.map((v, i) => [ks[i], v])
        return isMap
            ? new Map(ps)
            : Object.fromEntries(ps)
    })
}
```

On Sun, Oct 13, 2019 at 13:40 Cyril Auburtin <[hidden email]> wrote:
OP probably means he would like Promise.all to return an object as well, if an object if given

It's possible to hack an object to be iterable, but Promise.all already return an array unfortunately

On Sun, Oct 13, 2019 at 7:16 PM Boris Zbarsky <[hidden email]> wrote:
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
--

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Cyril Auburtin
maybe a naming like: `Promise.allObject`

```js
Promise.allObject = obj => {
  if (obj && !obj[Symbol.iterator]) {
    return Promise.all(
      Object.entries(obj).map(async ([k, v]) => [k, await v])
    )
      .then(Object.fromEntries);
  }
  return Promise.all(obj);
}

var delay = t => new Promise(r => setTimeout(r, t));
console.time(1); console.log(await Promise.allObject({foo: delay(110).then(()=>1), bar: delay(120).then(()=>2)})); console.timeEnd(1)
```

On Sun, Oct 13, 2019 at 8:55 PM Isiah Meadows <[hidden email]> wrote:
Maybe Promise.join(object)? Also, if a map is passed (or any iterable), it should be joined into a map.

At the most basic level:

```js
Promise.join = (o) => {
    let isMap = o[Symbol.iterator] != null
    let ps = (
        isMap ? Array.from : Object.entries
    )(o)
    let ks = ps.map(p => p[0])
    return Promise.all(ps.map(p => p[1]))
    .then(vs => {
        let ps = vs.map((v, i) => [ks[i], v])
        return isMap
            ? new Map(ps)
            : Object.fromEntries(ps)
    })
}
```

On Sun, Oct 13, 2019 at 13:40 Cyril Auburtin <[hidden email]> wrote:
OP probably means he would like Promise.all to return an object as well, if an object if given

It's possible to hack an object to be iterable, but Promise.all already return an array unfortunately

On Sun, Oct 13, 2019 at 7:16 PM Boris Zbarsky <[hidden email]> wrote:
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
--

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Michał Wadas
Established name is Promise.properties

On Mon, 14 Oct 2019, 09:29 Cyril Auburtin, <[hidden email]> wrote:
maybe a naming like: `Promise.allObject`

```js
Promise.allObject = obj => {
  if (obj && !obj[Symbol.iterator]) {
    return Promise.all(
      Object.entries(obj).map(async ([k, v]) => [k, await v])
    )
      .then(Object.fromEntries);
  }
  return Promise.all(obj);
}

var delay = t => new Promise(r => setTimeout(r, t));
console.time(1); console.log(await Promise.allObject({foo: delay(110).then(()=>1), bar: delay(120).then(()=>2)})); console.timeEnd(1)
```

On Sun, Oct 13, 2019 at 8:55 PM Isiah Meadows <[hidden email]> wrote:
Maybe Promise.join(object)? Also, if a map is passed (or any iterable), it should be joined into a map.

At the most basic level:

```js
Promise.join = (o) => {
    let isMap = o[Symbol.iterator] != null
    let ps = (
        isMap ? Array.from : Object.entries
    )(o)
    let ks = ps.map(p => p[0])
    return Promise.all(ps.map(p => p[1]))
    .then(vs => {
        let ps = vs.map((v, i) => [ks[i], v])
        return isMap
            ? new Map(ps)
            : Object.fromEntries(ps)
    })
}
```

On Sun, Oct 13, 2019 at 13:40 Cyril Auburtin <[hidden email]> wrote:
OP probably means he would like Promise.all to return an object as well, if an object if given

It's possible to hack an object to be iterable, but Promise.all already return an array unfortunately

On Sun, Oct 13, 2019 at 7:16 PM Boris Zbarsky <[hidden email]> wrote:
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
--
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Modify Promise.all() to accept an Object as a parameter

Michael Luder-Rosefield
The RSVP library uses Promise.hash, which seems sensible enough that I'm surprised no-one has mentioned or suggested it here.
--------------------------
Dammit babies, you've got to be kind.


On Mon, 14 Oct 2019 at 09:17, Michał Wadas <[hidden email]> wrote:
Established name is Promise.properties

On Mon, 14 Oct 2019, 09:29 Cyril Auburtin, <[hidden email]> wrote:
maybe a naming like: `Promise.allObject`

```js
Promise.allObject = obj => {
  if (obj && !obj[Symbol.iterator]) {
    return Promise.all(
      Object.entries(obj).map(async ([k, v]) => [k, await v])
    )
      .then(Object.fromEntries);
  }
  return Promise.all(obj);
}

var delay = t => new Promise(r => setTimeout(r, t));
console.time(1); console.log(await Promise.allObject({foo: delay(110).then(()=>1), bar: delay(120).then(()=>2)})); console.timeEnd(1)
```

On Sun, Oct 13, 2019 at 8:55 PM Isiah Meadows <[hidden email]> wrote:
Maybe Promise.join(object)? Also, if a map is passed (or any iterable), it should be joined into a map.

At the most basic level:

```js
Promise.join = (o) => {
    let isMap = o[Symbol.iterator] != null
    let ps = (
        isMap ? Array.from : Object.entries
    )(o)
    let ks = ps.map(p => p[0])
    return Promise.all(ps.map(p => p[1]))
    .then(vs => {
        let ps = vs.map((v, i) => [ks[i], v])
        return isMap
            ? new Map(ps)
            : Object.fromEntries(ps)
    })
}
```

On Sun, Oct 13, 2019 at 13:40 Cyril Auburtin <[hidden email]> wrote:
OP probably means he would like Promise.all to return an object as well, if an object if given

It's possible to hack an object to be iterable, but Promise.all already return an array unfortunately

On Sun, Oct 13, 2019 at 7:16 PM Boris Zbarsky <[hidden email]> wrote:
On 10/12/19 12:52 AM, Jacob Bloom wrote:
> const responses = await Promise.all(requests);

As opposed to:

   const responses = await Primise.all(requests.values());

which works right now?

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
--
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Modify Promise.all() to accept an Object as a parameter

Tab Atkins Jr.
In reply to this post by Jacob Bloom
On Fri, Oct 11, 2019 at 9:53 PM Jacob Bloom <[hidden email]> wrote:

> What about special handling for Maps? Maybe something like
>
> ```
> const requests = new Map();
> requests.set('reqA', fetch('...'));
> requests.set('reqB', fetch('...'));
> const responses = await Promise.all(requests);
> console.log(
>   responses.get('reqA'),
>   responses.get('reqB'),
> );
> ```
>
> ...which would continue to fail for most objects and would encourage
> using Map over POJOs for map-like data structures. (Since Maps are
> iterable, this may break some existing code but I doubt it)

Hm. Since the functor instance over Maps is just the values, and
Promise.all is just a special-cased traverse, I think this makes sense
to me:

    Map<keyType, Promise<valueType>> => Promise<Map<keyType, valueType>>

I'm fine with this living in another method name, tho.

~TJ
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss