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

p-map + p-queue #11

Closed
mahnunchik opened this issue Nov 11, 2020 · 1 comment
Closed

p-map + p-queue #11

mahnunchik opened this issue Nov 11, 2020 · 1 comment

Comments

@mahnunchik
Copy link

mahnunchik commented Nov 11, 2020

Hi @sindresorhus

Maybe I haven't catch right module but it would be great to have mix of p-map + p-queue to be able to run the same promise-returning & async function on inputs queue.

For example:

const queue = new Queue((item) => {
  return got('https://sindresorhus.com/?unicorn=' + item);
}, { concurrency: 2 });

(async () => {
  await queue.add('🦄');
  await queue.add(async () => '🦄');
  await queue.add('🐴');
  await queue.add( '🐙');
})();
@sindresorhus
Copy link
Owner

You don't need p-map for this. Just define a tiny helper method:

const {default: PQueue} = require('p-queue');
const got = require('got');

const queue = new PQueue({concurrency: 2});

const add = item => queue.add(() => got('https://sindresorhus.com', {searchParams: {unicorn: item}}));

(async () => {
	await add('🦄');
	await add(async () => '🦄');
	await add('🐴');
	await add( '🐙');
})();

If you just need to limit concurrency and don't need a queue, check out https://github.com/sindresorhus/p-limit

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