Last active
January 1, 2018 18:06
-
-
Save moinmoti/60bf844722bd9baab53cc97b457966a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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