sindresorhus / p-limit

Run multiple promise-returning & async functions with limited concurrency
MIT License
2.06k stars 106 forks source link

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

Closed andybee closed 2 years ago

andybee commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

🤦‍♂️ Thank you!