Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix setImmediate not triggering the timer if idle #2

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})