Skip to content

Instantly share code, notes, and snippets.

@danielstjules
Created April 11, 2016 20:26
Show Gist options
  • Save danielstjules/8b6291720c4eea9f3a4e7e6f712eea9b to your computer and use it in GitHub Desktop.
Save danielstjules/8b6291720c4eea9f3a4e7e6f712eea9b to your computer and use it in GitHub Desktop.
/**
* Why jQuery "promises" are bad
*/
// This is a promise that will print "A" after a setTimeout
var es6Promise = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('A');
resolve();
}, 0);
});
// Using only es6 promises
Promise.resolve().then(() => {
return es6Promise;
}).then(() => {
console.log('B');
});
// Prints A, B
// Mixing jQuery/es6 promises
var jQueryPromise = $.Deferred();
jQueryPromise.then(() => {
return es6Promise;
}).then(() => {
console.log('B');
});
jQueryPromise.resolve();
// Prints: B, A
// This is because the jQuery promise doesn't realize es6Promise is a promise,
// so it doesn't wait for it to finish execution. This makes it easy to
// introduce unintended race conditions or other side effects, since it's
// absolutely not how promises are supposed to work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment