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

If promises are generated ad-hoc, p-limit doesn't execute some of them #59

Closed
andybee opened this issue Feb 7, 2022 · 3 comments
Closed

Comments

@andybee
Copy link

andybee commented Feb 7, 2022

I have some code which is firing off promises on an "ad-hoc" basis. I need to limit how many are actually executing at the same time (else too many open network connections, etc.)

Using p-limit it never fires a promise if it comes in whilst all concurrent capacity is in-use.

A quick demo of this issue:

import delay from 'delay';
import pLimit from 'p-limit';

const limit = pLimit(1);

const wasteTime = async (value) => {
  await delay(1000);
  return value;
};

let count = 0;
setInterval(async () => {
  count += 1;
  console.log(await limit(() => wasteTime(count)));
}, 500);

I would expect to see output of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

etc.

But instead I see:

1
2
4
6
8
10
12
14

It looks like the functions called when the pool is busy never execute?

@sindresorhus
Copy link
Owner

Your counter is flawed. You must increase it when the function is actually executed.

import delay from 'delay';
import pLimit from './index.js';

const limit = pLimit(1);

const wasteTime = async (value) => {
  await delay(1000);
  return value;
};

let count = 0;
setInterval(async () => {
  console.log(await limit(() => wasteTime(++count)));
}, 500);

@sindresorhus
Copy link
Owner

If this is actually an issue with p-limit, it would be amazing if you would be willing to submit a failing test that shows it.

@andybee
Copy link
Author

andybee commented Feb 25, 2022

🤦‍♂️ Thank you!

@andybee andybee closed this as completed Feb 25, 2022
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