diff --git a/packages/scheduler/src/forks/SchedulerDOM.js b/packages/scheduler/src/forks/SchedulerDOM.js index 53101a6e6e58..e872b3ae6590 100644 --- a/packages/scheduler/src/forks/SchedulerDOM.js +++ b/packages/scheduler/src/forks/SchedulerDOM.js @@ -519,21 +519,25 @@ const performWorkUntilDeadline = () => { // the message event. deadline = currentTime + yieldInterval; const hasTimeRemaining = true; + + // If a scheduler task throws, exit the current browser task so the + // error can be observed. + // + // Intentionally not using a try-catch, since that makes some debugging + // techniques harder. Instead, if `scheduledHostCallback` errors, then + // `hasMoreWork` will remain true, and we'll continue the work loop. + let hasMoreWork = true; try { - const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); - if (!hasMoreWork) { - isMessageLoopRunning = false; - scheduledHostCallback = null; - } else { + hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); + } finally { + if (hasMoreWork) { // If there's more work, schedule the next message event at the end // of the preceding one. port.postMessage(null); + } else { + isMessageLoopRunning = false; + scheduledHostCallback = null; } - } catch (error) { - // If a scheduler task throws, exit the current browser task so the - // error can be observed. - port.postMessage(null); - throw error; } } else { isMessageLoopRunning = false; diff --git a/packages/scheduler/src/forks/SchedulerPostTaskOnly.js b/packages/scheduler/src/forks/SchedulerPostTaskOnly.js index ba081d3236e5..8ee96e2d16e5 100644 --- a/packages/scheduler/src/forks/SchedulerPostTaskOnly.js +++ b/packages/scheduler/src/forks/SchedulerPostTaskOnly.js @@ -512,21 +512,25 @@ const performWorkUntilDeadline = () => { // the message event. deadline = currentTime + yieldInterval; const hasTimeRemaining = true; + + // If a scheduler task throws, exit the current browser task so the + // error can be observed. + // + // Intentionally not using a try-catch, since that makes some debugging + // techniques harder. Instead, if `scheduledHostCallback` errors, then + // `hasMoreWork` will remain true, and we'll continue the work loop. + let hasMoreWork = true; try { - const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); - if (!hasMoreWork) { + hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); + } finally { + if (hasMoreWork) { + // If there's more work, schedule the next browser task at the end of + // the preceding one. + postTask(performWorkUntilDeadline); + } else { isTaskLoopRunning = false; scheduledHostCallback = null; - } else { - // If there's more work, schedule the next message event at the end - // of the preceding one. - postTask(performWorkUntilDeadline); } - } catch (error) { - // If a scheduler task throws, exit the current browser task so the - // error can be observed. - postTask(performWorkUntilDeadline); - throw error; } } else { isTaskLoopRunning = false;