Created
June 9, 2014 20:37
-
-
Save fabioyamate/d123a4b392844bec7983 to your computer and use it in GitHub Desktop.
Promise polyfill with jQuery Deferreds
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 Promise(fn) { | |
this.promise = $.Deferred(function(dfd) { | |
fn(dfd.resolve, dfd.fail); | |
}).promise(); | |
} | |
Promise.prototype.then = function(done, fail) { | |
this.promise.then(done, fail); | |
}; | |
var promise = new Promise(function(resolve, fail) { | |
setTimeout(function() { | |
resolve(); | |
}, 3000); | |
}); | |
promise.then(function() { | |
console.log('done!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment