Collected · Vue · MIT

Temporary exhibition · Hall of the Commons

Four Ways to Wait

JavaScript·2019·110 lines·3673 bytes

Curator’s note

Every framework needs somewhere to put work that must happen soon but not now. This is Vue's. Change some data, and the DOM does not update on the next line — it updates on the next tick, and this file decides when that is.

Most of it is not code. Forty lines do the job; the rest is the record of finding out what the job was. timerFunc is chosen once, at load, by trying four things in order: native Promise, then MutationObserver, then setImmediate, then setTimeout. That is a walk down every mechanism JavaScript ever offered for deferring work, in the order they arrived, and the comments say who broke each one. UIWebView on iOS 9.3.3, where Promise.then gets stuck in "a weird state." PhantomJS. iOS 7. Android 4.4. IE11, where MutationObserver is unreliable. Eleven issue numbers are cited inline, as evidence.

Two lines are worth standing in front of. The first is if (isIOS) setTimeout(noop) — an empty timer, scheduled for no reason except to make a browser notice that it has stopped flushing its own microtask queue. A workaround for a workaround, four words long. The second is the comment above timerFunc, which records a decision being reversed: 2.5 used macrotasks, and 2.6 went back to microtasks everywhere, "again". The paragraph then sets out what that cost, with issue numbers, rather than claiming it was free.

The file also exports isUsingMicroTask — a confession about which of the four it settled on, so that code elsewhere in the framework can compensate for the answer.

Almost everything in this museum shows a good idea. This shows the other half of the work: what it costs to make a good idea survive contact with browsers that are wrong. It is finished, too. Vue 2 was retired at the end of 2023, and apart from being translated into TypeScript in 2021 this file had not changed in four years. Nothing more will be added to it.

Related: why a queue is not the same as doing two things at once, and what it costs to change a decision after people depend on it.