diff --git a/index.js b/index.js index efd5107..57d8261 100644 --- a/index.js +++ b/index.js @@ -251,6 +251,7 @@ function ontimer () { function onimmediate () { const now = Date.now() + const timerRunning = queue.length > 0 let uncaughtError = null @@ -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 } diff --git a/test/immediate.js b/test/immediate.js index 8a2e7d3..abed1bf 100644 --- a/test/immediate.js +++ b/test/immediate.js @@ -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) + }) +})