A test saves a note in Shelf and then waits half a second before it checks. It passes on the author's laptop. On the CI server it fails about one run in ten.
setTimeout is the function behind every such pause. It takes a callback and a number of milliseconds. It asks the host to run the callback after that time. The host is the program that runs your JavaScript, which is the browser or Node.
console.log("first");
setTimeout(() => {
console.log("from the timer");
}, 0);
console.log("last");
first
last
from the timer
The delay is 0, and the timer line still prints last. A timer callback never interrupts code that is running. The host runs it only after the current code has finished. Both browsers and Node guarantee this order. It is a rule of the host, not of the JavaScript language, because timers belong to the host.
A pause is a guess about speed
Here is the saving test as plain code. The save takes 30 milliseconds this time.
let stored = "draft";
function saveNote(text) {
setTimeout(() => {
stored = text;
}, 30);
}
saveNote("final");
setTimeout(() => {
console.log("stored: " + stored);
}, 50);
stored: final
The check waits 50 milliseconds, the save needs 30, and the test passes. Nothing in this code connects the two numbers. The 50 is a guess that the save will be faster than that.
On a slow day the save needs 80. The check still runs at 50 and reads "draft". On a fast day the save needs 5, and the test waits 45 milliseconds for nothing. Across a few hundred tests, those pauses add minutes to every run.
We do not show the slow run as an example you can run. Its result would depend on real timing, and an example whose output depends on timing proves nothing. The exercise for this lecture controls the clock itself, so the slow case fails every time.
Wait for the work, not for the clock
The first way JavaScript solved this was a completion callback. The function that starts the work takes a second callback and calls it when the work is done.
let stored = "draft";
function saveNote(text, onSaved) {
setTimeout(() => {
stored = text;
onSaved();
}, 30);
}
saveNote("final", () => {
console.log("stored: " + stored);
});
stored: final
Now the check runs when the save finishes. If the save needs 5 milliseconds, the check runs after 5. If it needs 80, the check runs after 80. There is no number to tune.
Callbacks have a cost. When one step must follow another, each callback sits inside the one before it, and the code moves further to the right with every step. Errors are also hard to pass along. Promises were added to the language to fix both problems, and they are the next lecture.
Older Node code and some libraries still use completion callbacks, so you need to be able to read them.
What this means for a test
Testing tools give you waits that are tied to the work. Playwright's expect(locator).toHaveText() keeps checking until the text is there. Cypress retries should. Selenium has explicit waits for a condition. Each of them plays the part of onSaved: it ends when the thing you care about has happened.
A fixed pause in a pull request usually gets one comment from a reviewer: "what are we waiting for?" If you can name it, wait for that.
Remember this
A timer callback runs after the current code, even with a delay of 0. A fixed pause is a guess that fails when the work is slow and wastes time when it is fast. Wait for the work to finish, not for an amount of time.
Reference: setTimeout on MDN.