Skip to content

Instantly share code, notes, and snippets.

@moinmoti
Last active January 1, 2018 18:06
Show Gist options
  • Save moinmoti/60bf844722bd9baab53cc97b457966a1 to your computer and use it in GitHub Desktop.
Save moinmoti/60bf844722bd9baab53cc97b457966a1 to your computer and use it in GitHub Desktop.
function createSleepPromise(timeout) {
return new Promise(function(resolve) {
setTimeout(resolve, timeout);
});
};
function sleep(timeout) {
// Pass value through, if used in a promise chain
function promiseFunction(value) {
return createSleepPromise(timeout).then(function() {
return value;
});
};
// Normal promise
promiseFunction.then = function() {
var sleepPromise = createSleepPromise(timeout);
return sleepPromise.then.apply(sleepPromise, arguments);
};
promiseFunction.catch = Promise.resolve().catch;
return promiseFunction;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment