Skip to content

Commit ed58372

Browse files
committed
Fix "Illegal invocation" error with custom timers
Fixes #45
1 parent f9ced5e commit ed58372

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export default function pTimeout(promise, options) {
5050
// We create the error outside of `setTimeout` to preserve the stack trace.
5151
const timeoutError = new TimeoutError();
5252

53-
timer = customTimers.setTimeout(() => {
53+
// `.call(undefined, ...)` is needed for custom timers to avoid context issues
54+
timer = customTimers.setTimeout.call(undefined, () => {
5455
if (fallback) {
5556
try {
5657
resolve(fallback());
@@ -85,7 +86,8 @@ export default function pTimeout(promise, options) {
8586
});
8687

8788
cancelablePromise.clear = () => {
88-
customTimers.clearTimeout(timer);
89+
// `.call(undefined, ...)` is needed for custom timers to avoid context issues
90+
customTimers.clearTimeout.call(undefined, timer);
8991
timer = undefined;
9092
};
9193

0 commit comments

Comments
 (0)