Skip to content

Instantly share code, notes, and snippets.

@getify
Last active December 20, 2015 19:39
Show Gist options
  • Save getify/5d5e0090cf446248c724 to your computer and use it in GitHub Desktop.
Save getify/5d5e0090cf446248c724 to your computer and use it in GitHub Desktop.
exploring async+sync promises resolution from: http://dom.spec.whatwg.org/#concept-promise-wrapper-callback
// we expect this to print out "3", not "2", thus the
// promises implementation needs to "delay" the
// running of that first step, to create consistent
// ordering. Thus, line 16 runs before line 13.
var ASQ = require("asynquence");
var a = 2;
ASQ()
.then(function(){
console.log(a);
});
a++;
// by the same reasoning as above, we would expect
// this to print out "3" and then "4". However,
// if `asynquence` were to do the async+sync
// resolution as described here (IIUC):
// http://dom.spec.whatwg.org/#concept-promise-wrapper-callback
//
// ...we'd instead get "3" and then "3", because
// the sequence would be put in the "synchronous"
// mode after line 23, so the callback for line 26-29
// would run synchronously, thus running before line
// 31. This violates the assumption (from above) that
// expectedly lead to line 34 running before line 22.
var ASQ = require("asynquence");
var a = 2;
var seq =
ASQ()
.then(function(done){
console.log(a);
done(); // "resolve" this step's promise right away
seq
.then(function(done){
console.log(a);
a++;
});
a++;
});
a++;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment