-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Conversation
a2359bd
to
2ac3367
Compare
|
||
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. |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use t.is
try { | ||
endValue = await promise; | ||
} catch (error_) { | ||
error = error_; | ||
} |
There was a problem hiding this comment.
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); | |||
} |
There was a problem hiding this comment.
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.
if (signal) { | ||
signal.addEventListener('abort', throttled.abort); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs import statement
Add
signal
option to listen to execution abortion. When the signal is aborted, all future and pending calls are aborted.