p-queue icon indicating copy to clipboard operation
p-queue copied to clipboard

Fix timeout-dependent return types of `add` and `addAll`

Open lukehorvat opened this issue 2 weeks ago • 0 comments

Hi. The return types of add and addAll are currently a bit funky. I think there was a regression.

For example, the following code raises a type error for b because it thinks a is number | void:

const a = await this.queue.add(async () => 1);
const b: number = a * 2;

That doesn't seem right. It should only return number | void when both timeout=number and throwOnTimeout=false|undefined. But in the example above I haven't even specified timeout, so the void behavior should never surface.

This PR fixes the return types:

  • When timeout=number and throwOnTimeout=true, return type is Thing.
  • When timeout=number and throwOnTimeout=false|undefined, return type is Thing | void.
  • When timeout=undefined and throwOnTimeout=true|false|undefined, return type is Thing.

To further emphasize the relationship between timeout and throwOnTimeout I also added a note in the readme and a warning message in the console.

lukehorvat avatar Jun 21 '24 20:06 lukehorvat