Skip to content

timers distribution predictability#15081

@vsemozhetbyt

Description

@vsemozhetbyt
  • Version: 8.4.0
  • Platform: Windows 7 x64
  • Subsystem: libuv, promises

Revisiting this article, I've found out that an example which illustrates some known bug (?) in microtask queue execution behaves differently because of times of script call.

'use strict';console.log('script start')constinterval=setInterval(()=>{console.log('setInterval')},0)setTimeout(()=>{console.log('setTimeout 1')Promise.resolve().then(()=>{console.log('promise 3')}).then(()=>{console.log('promise 4')}).then(()=>{setTimeout(()=>{console.log('setTimeout 2')//< place of random microtask queue intrusionPromise.resolve().then(()=>{console.log('promise 5')}).then(()=>{console.log('promise 6')}).then(()=>{clearInterval(interval)})},0)})},0)Promise.resolve().then(()=>{console.log('promise 1')}).then(()=>{console.log('promise 2')})

Every first call in a new cmd.exe terminal produces the mentioned error. Every next call has proper execution order:

>node test.jsscript startpromise 1promise 2setIntervalsetTimeout 1promise 3promise 4setIntervalsetTimeout 2setInterval //< intruderpromise 5promise 6>node test.jsscript startpromise 1promise 2setIntervalsetTimeout 1promise 3promise 4setIntervalsetTimeout 2promise 5promise 6

Compare the next step after the setTimeout 2 output.

Metadata

Metadata

Assignees

Labels

promisesIssues and PRs related to ECMAScript promises.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions