Created
October 18, 2015 02:08
-
-
Save jaawerth/4fb4ea5bee96aa84aa3b to your computer and use it in GitHub Desktop.
Example of how to write a version of setTimeout that returns a promise
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 timeout(fn, delay) { | |
var args = Array.prototype.slice(arguments, 2); | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
try { | |
resolve(fn.apply(null, args)); | |
} catch(e) { | |
reject(e); | |
} | |
}, delay); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment