sindresorhus/p-limit

Should clearQueue() reject the pending promises?

jedwards1211 opened this issue · 6 comments

Right now it leaves them hanging indefinitely. It seems like undesirable behavior, though I guess it ultimately depends on the use case.

There's no way to reject promises from the outside. We could maybe support cancelling them if they are of the type p-cancelable.

There's no way to reject promises from the outside

I take it you mean promises returned by the user-supplied function, but I'm not talking about those, I'm talking about the promises p-limit created for queued functions that haven't even been called yet:

const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args));
                                       ^^^^^^^

We could change this to

const generator = (fn, ...args) => new Promise((resolve, reject) => enqueue(fn, resolve, reject, ...args));

And refactor the queue entries to include reject (instead of just being the bound function), so that clearQueue() can call reject on all the queue entries.

Could you please add an example of how to clearQueue() on error? I cannot figure it out. No matter when/where I try to call limit.clearQueue() I get TypeError: limit.clearQueue is not a function

@RichardBronosky maybe you have an old version installed? Looks like clearQueue was only added 3 months ago.

I think it would be better to support AbortController and let the user decide what to do about the signal.

Reading the docs, I assumed this:

try {
   await Promise.all(things.map(async (thing) => limit(async () => {/*...*/}) ));
} catch (error) {
  limit.clearQueue();
  throw error;
}

would prevent promises not yet queued from running.

For example, if things.length === 1000 and limit is 3 and one of the first 3 promises throws, the queue is emptied and the remaining 997 other things and their Promises are not executed.

Is this correct?