Skip to content

Instantly share code, notes, and snippets.

@getify
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/9117527 to your computer and use it in GitHub Desktop.
Save getify/9117527 to your computer and use it in GitHub Desktop.
Trying to illustrate the sync/async inconsistency between promise initialization and promise chaining.
// For consistency sake, would expect:
// 1, 2, 3, 4, 5
// actually gets, confusingly:
// 1, 2, 4, 3, 5
var p = new Promise(function(resolve,reject){
resolve();
});
// ****************************************
console.log("1");
// .then callbacks resolve async, even if the promise is immediately fulfilled
// so that we normalize sync and async logic.
// ...
// So, we just think of "everything" as "async", right? No.
p.then(function(){
// But Promise construction is sync, not async, which creates unexpected
// sync in the middle of all this async.
// ...
// :(
var p2 = new Promise(function(resolve,reject){
console.log("4"); // "4" comes before "3", obviously </sarcasm>
resolve();
});
console.log("3");
return p2;
})
.then(function(){
console.log("5");
});
console.log("2");
// In other words, if "2" comes before "3", why doesn't "3" come before "4"?
// ...
// THAT is the seeming inconsistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment