Skip to content

Instantly share code, notes, and snippets.

@fabioyamate
Created June 9, 2014 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioyamate/d123a4b392844bec7983 to your computer and use it in GitHub Desktop.
Save fabioyamate/d123a4b392844bec7983 to your computer and use it in GitHub Desktop.
Promise polyfill with jQuery Deferreds
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