Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Created October 18, 2015 02:08
Show Gist options
  • Save jaawerth/4fb4ea5bee96aa84aa3b to your computer and use it in GitHub Desktop.
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
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