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

feat: support AbortSignal to listen to execution abortion #48

Closed
wants to merge 2 commits into from

Conversation

krisanalfa
Copy link

Add signal option to listen to execution abortion. When the signal is aborted, all future and pending calls are aborted.


Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

An optional signal to listen for abort events. If the signal becomes aborted, all pending and future calls are rejected with a `pThrottle.AbortError` error.
Copy link
Owner

@sindresorhus sindresorhus Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct behavior. It should reject with AbortSignal.reason


t.true(error instanceof AbortError);
t.true(end() < 100);
t.true(one === 1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.is

Comment on lines +166 to +170
try {
endValue = await promise;
} catch (error_) {
error = error_;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.throwsAsync

@@ -109,6 +109,10 @@ export default function pThrottle({limit, interval, strict, onDelay}) {
},
});

if (signal) {
signal.addEventListener('abort', throttled.abort);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to use AbortSignal.throwIfAborted() as early as possible in the function in case the signal was already aborted when this is called.

Comment on lines +112 to +114
if (signal) {
signal.addEventListener('abort', throttled.abort);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (signal) {
signal.addEventListener('abort', throttled.abort);
}
signal?.addEventListener('abort', throttled.abort);

`AbortSignal` to abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.

@example
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs import statement

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

Successfully merging this pull request may close these issues.

2 participants