Skip to content

Instantly share code, notes, and snippets.

@deepak
Created February 2, 2015 11:30
Show Gist options
  • Save deepak/dd2e505b7ff71121fd9b to your computer and use it in GitHub Desktop.
Save deepak/dd2e505b7ff71121fd9b to your computer and use it in GitHub Desktop.
javascript ES6 promise is not executed in serial order
var flag = false;
var promise = Promise.resolve();
// not linear execution of code!
// is the then callback pushed onto the call-stack. how ?
promise.then( (x) => {
console.log(`flag is ${flag}`); // flag is true
})
flag = true
@deepak
Copy link
Author

deepak commented Feb 2, 2015

noob code to shim a promise. does not work

class DummyPromise {
  then(x) {
    x.call();
  }
}
var flag = false;
var stub = new DummyPromise();

() => {
  () => {
    console.log(`flag is ${flag}`);  
  }()
  console.log(`flag is ${flag}`);  
}()

stub.then(() => {
  console.log(`flag is ${flag}`);
})

flag = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment