Skip to main content
Post Closed as "Duplicate" by Bergi javascript
edited title
Link
Asking
  • 4.3k
  • 21
  • 84
  • 171

Order of execution in JavascripJavascript

Source Link
Asking
  • 4.3k
  • 21
  • 84
  • 171

Order of execution in Javascrip

I have this code:

setTimeout(function timeout() { console.log('timeout'); }, 0); let p = new Promise(function(resolve, reject) { console.log('create promise'); resolve(); }); p.then(function(){ console.log('execute promise'); }); console.log('end');

I get this order when execute the code:

  • create promise
  • end
  • execute promise
  • timeout


Question: Why create promise is executed first? I expect to execute first end, because it is the only one synchrone code so it should be first executed.