-
Notifications
You must be signed in to change notification settings - Fork 14
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
Is each mapFn
executed concurrently or sequentially?
#42
Comments
Yes, for await (const v of array) {
newArray.push(await mapFn(v));
} You can easily achieve parallel filtering. const filteredArray = Promise.all(array.map(async (v) => [v, await callAPI(v)])).then((a) => a.filter(([, c]) => c).map(([v]) => v)); |
What @Josh-Cena says is accurate, but for your specific application, the stage 2 async iterator helpers proposal is likely more appropriate - it will let you write let filtered = await array.values().toAsync().filter(someAsyncPredicate).toArray(); This will be sequential, though you will be able to specify a degree of concurrency with something like let filtered = await array.values().toAsync().filter(someAsyncPredicate).bufferAhead(5).toArray(); |
Thanks for the responses, it sounds like I can most easily achieve my goals by using the |
Here is a code snippet of what I implemented if anyone is interested: for await (const invoice of invoices) {
const validated = await validateInvoice(invoice);
if (validated) {
invoicesFiltered.push(invoice);
}
} |
I am interested in this proposal but would like clarification on the above. My use-case would be to filter a current Array using an async
mapFn
to return a new Array with only the values that passed the filter function.I would want this to happen sequentially because the
mapFn
would call an API. I attempted to do this with a regularArray.filter()
method but discovered that you cannot use this method using async functions because they will always be truthy.Please let me know if this new proposal will work for my intended use-case, if so I would like to try out your polyfill package. Thanks!
The text was updated successfully, but these errors were encountered: