Skip to content

setTimeout is still too inaccurate #6037

@ChALkeR

Description

@ChALkeR

Previously: #6019

This is on compatibilityDate = "2026-02-07"

export default {
  async test(ctrl, env, ctx) {
    const t0 = Date.now()
    console.log('A', t0, Date.now() - t0)

    await new Promise((accept) => setTimeout(accept, 50))
    console.log('B', t0, Date.now() - t0)

    for (let j=0; j<1e9; j++); // this is not actually required if something else is going on

    await new Promise((accept) => setTimeout(accept, 50))
    console.log('C', t0, Date.now() - t0)

    await new Promise((accept) => setTimeout(accept, 50))
    console.log('D', t0, Date.now() - t0)

    await new Promise((accept) => setTimeout(accept, 50))
    console.log('E', t0, Date.now() - t0)

    await new Promise((accept) => setTimeout(accept, 50))
    console.log('F', t0, Date.now() - t0)
  }
}

Output:

A 1770454880363 0
B 1770454880363 55
C 1770454880363 398
D 1770454880363 398
E 1770454880363 398
F 1770454880363 398

Why do C-D-E-F all fire at the same time when they should be 50ms apart?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions