Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 10
Where the error goes
Unhandled promise rejections and try/catch in async code
Everything is computed in your browser; nothing is sent anywhere.
About this experiment
What is an unhandled promise rejection?
An unhandled promise rejection happens when a promise is rejected and nothing is attached to handle it: no .catch, no second argument to .then, no await inside a try. In a browser the window receives an unhandledrejection event and the page keeps running. Node is stricter. Since version 15 its default is to end the process with an error, so the rest of the program never runs. The cure is to give every promise someone who waits for its result.
An ordinary try/catch catches errors thrown while it is running. Asynchronous code throws later, after the try block has finished, and the error has to find another way out. The experiment takes four short programs from the book, each of which tries to read a file that does not exist: one with a callback, one with a promise chain, one with async/await, and one that starts the read and does not wait for it.
Pick a tab and press “Run”. The program runs in this tab, and four boxes show where the error ended up: in the try around the call, in the .catch at the end of the chain, in the try around await, or with nobody, as an event on window. The widget listens for those window events while the program runs and calls preventDefault() on its own errors, so nothing spills into the console. A browser has no fs, so a setTimeout stands in for the file callback and readFile is a stub that rejects with ENOENT the way Node does.
Beside the browser log is what the same code did in the book, in Node 24. The callback version printed “try finished without errors” and then the process crashed with exit code 1. The .catch at the end of the chain caught ENOENT. An ordinary try/catch around await caught ENOENT. The rejected promise that nobody waited for crashed the process with exit code 1, and the message from the 100 ms timer was never printed. In the browser that timer fires, because a page survives what ends a Node process.
The chapter follows who keeps the rest of the program. In synchronous code the stack keeps it. Callbacks handed that job to the programmer, and try/catch went with it. Promises and await gave it back to the runtime, so try/catch works again, and await marks in the text each place where the function lets go of control.
From the book
JavaScript: From Closures to Autonomous Systems · Chapter 10
Continuations
They gave it back, though, with a caveat that matters for the next chapter. The places where a function gives up control are marked with the word await, and the programmer is responsible for them: between two lines with await, data read earlier may go stale. Meteor went further and removed even that word. Server code in Meteor looked completely synchronous, and the runtime stored continuations whole, together with the stack. Why this sped up development so much and why it still had to be abandoned is told in the eleventh chapter.