Skip to content

Commit

Permalink
Clear existing timeouts when an abort signal aborts (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronami committed Jun 12, 2023
1 parent 9260b09 commit 52d1a6d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.js
Expand Up @@ -45,7 +45,7 @@ export default function pTimeout(promise, options) {

let timer;

const cancelablePromise = new Promise((resolve, reject) => {
const wrappedPromise = new Promise((resolve, reject) => {
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
}
Expand Down Expand Up @@ -99,12 +99,14 @@ export default function pTimeout(promise, options) {
resolve(await promise);
} catch (error) {
reject(error);
} finally {
customTimers.clearTimeout.call(undefined, timer);
}
})();
});

const cancelablePromise = wrappedPromise.finally(() => {
cancelablePromise.clear();
});

cancelablePromise.clear = () => {
customTimers.clearTimeout.call(undefined, timer);
timer = undefined;
Expand Down

0 comments on commit 52d1a6d

Please sign in to comment.