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

Adding async array prototype methods #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

jeremygottfried
Copy link
Owner

Objective

Add async methods that replicate array prototype iterators.

  • aForEach
  • aMap
  • aReduce
  • aFilter
  • aEvery

These methods will have similar functionality to array prototypes iterators, but they can additionally carry out asynchronous tasks in order.

For example, the aReduce method will allow you to reduce an array asynchronously, which is currently not possible with array prototype.

const arr = [0,1,2,3];

async function callback(acc, current) {
  await new Promise(resolve => setTimeout(resolve, 0));
  return acc + current;
}

const reduce = aReduce(arr, callback); // => 6

Why

Most native array prototype iterators do not work well with async.
With forEach, you can schedule async tasks, but they will not occur in order.
With map, it returns an array of promises.
reduce doesn't work at all with async values.

User Experience

These methods will give users a more familiar interface for working with asynchronous iteration. Most javascript developers don't work with while loops on a regular basis.

@jeremygottfried jeremygottfried added the enhancement New feature or request label Mar 17, 2020
@jeremygottfried jeremygottfried self-assigned this Mar 17, 2020
@jeremygottfried jeremygottfried changed the title Adding async methods Adding async array prototype methods Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant