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

Is it possible to use in a chain? #15

Open
lmcarreiro opened this issue Sep 18, 2019 · 1 comment
Open

Is it possible to use in a chain? #15

lmcarreiro opened this issue Sep 18, 2019 · 1 comment

Comments

@lmcarreiro
Copy link

lmcarreiro commented Sep 18, 2019

It is common in libraries like this, to have a chain method, to allow you to use the methods in a chain without aux variables or putting one call inside the other.

Example:

const { chain } = require('p-iteration');

const usersIds = [1, 2, 3];
const users = await chain(usersIds)
  .map(async id => await getById(id))
  .filter(async user => await checkIsActive(user.anotherId))
  .value()

Is it possible to use an async chain?

@toniov
Copy link
Owner

toniov commented Oct 4, 2019

Hey sorry for the late reply!

Currently that's not possible, but it could be a good idea for a new version of the module.

Anyway, since each method returns a Promise you can chain them like you'd do normally, using your example:

const { map, filter } = require('p-iteration');

const usersIds = [1, 2, 3];
const users = await map(usersIds, user => usergetById(id))
  .then(users => filter(users,  user => checkIsActive(user.anotherId)));

It's not so smooth though, for this case just going with sequential awaits is a bit simpler I think:

const users = await map(usersIds, user => usergetById(id));
const filteredUsers = await filter(users,  user => checkIsActive(user.anotherId)));

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

No branches or pull requests

2 participants