Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map an asynchronous function? #2

Open
tsani opened this issue Mar 1, 2021 · 2 comments
Open

map an asynchronous function? #2

tsani opened this issue Mar 1, 2021 · 2 comments

Comments

@tsani
Copy link

tsani commented Mar 1, 2021

The type of .map() is

(selector: (x: T) => S): AsyncIterableQuery<S>

but I would have expected

(selector: (x: T) => Promise<S>): AsyncIterableQuery<S>

allowing the transformation of each element to be asynchronous.

Maybe there's a way to pull this off using .flat() but I can't seem to make it work. Any ideas?

@tsani
Copy link
Author

tsani commented Mar 1, 2021

Quick update: I did find a workaround with .flat() but it's kinda gnarly.

seq.flat(async function* (x) { yield await f(x) })

Surely seq.map(f) would be way nicer.

And this workaround is especially unpleasant if f needs to access this, as one can't use an arrow function to create a generator.

EDIT: and unfortunately, the overload for .flat() that takes a transformation returns a plain AsyncIterable and not an AsyncIterableQuery, so I can't continue to chain itiriri methods together after using the workaround without wrapping up with itiririAsync again.

@dimadeveatii
Copy link
Member

Hi @tsani,
I don't remember the exact implementation, but from the source code here it looks like map accepts async functions as well.

So in the case of:

(selector: (x: T) => S): AsyncIterableQuery<S>

the selector can be a function that returns a Promise. I agree typings have to be fixed.
It should look like:

(selector: (x: T) => S | Promise<S>): AsyncIterableQuery<S>

You're welcome to open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants