Skip to content

Commit

Permalink
Allow sinon.useFakeTimers() to modify setTimeout() and clearTimeout()…
Browse files Browse the repository at this point in the history
… after module loads (#45)
  • Loading branch information
Torgen authored and sindresorhus committed Jun 12, 2019
1 parent e9b049e commit 1316fd7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const createAbortError = () => {
return error;
};

const createDelay = ({clearTimeout: clear = clearTimeout, setTimeout: set = setTimeout, willResolve}) => (ms, {value, signal} = {}) => {
const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve}) => (ms, {value, signal} = {}) => {
if (signal && signal.aborted) {
return Promise.reject(createAbortError());
}

let timeoutId;
let settle;
let rejectFn;
const clear = defaultClear || clearTimeout;

const signalListener = () => {
clear(timeoutId);
Expand All @@ -37,7 +38,7 @@ const createDelay = ({clearTimeout: clear = clearTimeout, setTimeout: set = setT
};

rejectFn = reject;
timeoutId = set(settle, ms);
timeoutId = (set || setTimeout)(settle, ms);
});

if (signal) {
Expand Down

0 comments on commit 1316fd7

Please sign in to comment.