Read more

Tasks, microtasks, queues and schedules - JakeArchibald.com

Henning Koch
June 28, 2016Software engineer at makandra GmbH

The way that Javascript schedules timeouts and promise callbacks is more complicated than you think. This can be the reason why callbacks are not executed in the order that they are queued.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Please read this article!


This is an extract of the example in the article which demonstrates the execution order of tasks and microtasks.

console.log('script start');

setTimeout(function() {
  console.log('setTimeout');
}, 0);

Promise.resolve().then(function() {
  console.log('promise1');
}).then(function() {
  console.log('promise2');
});

console.log('script end');
// Task
script start
script end

// Microtask
promise1
promise2

// Task
setTimeout

The summary of the article says:

  • Tasks execute in order, and the browser may render between them

  • Microtasks execute in order, and are executed:

    • after every callback, as long as no other JavaScript is mid-execution
    • at the end of each task

Further resources:

Posted by Henning Koch to makandra dev (2016-06-28 19:23)