This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
.async
helper for getting an async iterator from a sync iterator
#160
Comments
It's like |
I don't think so. I think this helper is useful, let's add this function since we have |
toAsync sounds good. will add it in the next round |
zloirock
added a commit
to zloirock/core-js
that referenced
this issue
Dec 3, 2021
zloirock
added a commit
to zloirock/core-js
that referenced
this issue
Dec 9, 2021
michaelficarra
added
the
good follow-on proposal
this would be good but doesn't need to be in the first milestone
label
Jul 5, 2022
Closed
bakkot
removed
the
good follow-on proposal
this would be good but doesn't need to be in the first milestone
label
Jun 3, 2023
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I realize it's probably past the point where we want to add new methods, but I wanted to write this down here so we will at least have discussed it, and maybe pick it up in a later proposal. (Sidebar: time for a
future proposal
label, maybe?)In some code I was writing today, I had an array I wanted to
filter
and thenflatMap
. The problem was that the mapper function was async (i.e., it returned a promise of an array), so I couldn't useArray.prototype.flatMap
(which requires the mapper to return an array). What I first wrote wasbut then I realized I really wanted to do the async operations in sequence, not in parallel. There's no good way to do that without a manual
for await
loop right now - which is frustrating, since this really is most clearly expressed as afilter
followed by aflatMap
.With this proposal in its current state, I could instead write
which is better than a
for await
, but still kind of awkward because of the weirdAsyncIterator.from
in the middle (or rather not in the middle; you have to read carefully to see the point at which it applies). So it seems like it might be useful to have anasync
method on the sync iterator prototype which did the wrapping for you, such that you could doObviously there's a bunch of other ways to write this, especially if we get some form of pipeline. But this does feel like it's a primitive operation on sync iterators, and so might belong on
Iterator.prototype
.Edit a few months later: I needed this again in a simpler case. I wanted to do a
filter
, but my filtering function was async. It would have been nice to doawait arr.values().toAsync().filter(x => await foo(x)).toArray()
. This is probably a more obvious example thanflatMap
.The text was updated successfully, but these errors were encountered: