Skip to content

Commit

Permalink
Fix setImmediate() not triggering the timer if idle (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasper Isager Dalsgarð <kasperisager@hey.com>
  • Loading branch information
mafintosh and kasperisager authored Oct 1, 2024
1 parent bcce5a2 commit 4e4c72b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ function ontimer () {

function onimmediate () {
const now = Date.now()
const timerRunning = queue.length > 0

let uncaughtError = null

Expand All @@ -267,6 +268,13 @@ function onimmediate () {

tick()

// some reentry wants the timers to start but they are not, lets start them
if (!timerRunning && queue.length > 0) {
const l = queue.peek()
nextExpiry = l.expiry
updateTimer(l.ms)
}

if (uncaughtError !== null) {
throw uncaughtError
}
Expand Down
12 changes: 12 additions & 0 deletions test/immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,15 @@ test('setImmediate following setTimeout', async function (t) {

clearTimeout(timer)
})

test('setImmediate that triggers setTimeout', async function (t) {
t.plan(2)

timers.setImmediate(() => {
t.pass('immediate triggered')

timers.setTimeout(() => {
t.pass('timeout triggered')
}, 20)
})
})

0 comments on commit 4e4c72b

Please sign in to comment.