IsConnected Guards Stale Updates
Row updates that settle after their element was removed must check isConnected first, the cheap guard against writing into detached nodes.
Code
// After an await, the element may already be gone — isConnected guards
// stale async updates before they touch detached nodes
const list = document.querySelector('#feed');
async function renderItem(item) {
const row = document.createElement('li');
row.textContent = item.title;
list.append(row);
await Promise.resolve(); // any async gap: fetch, timer, user event
if (!row.isConnected) {
console.log('dropped stale update:', item.title);
return;
}
row.classList.add('loaded');
console.log('applied update:', item.title);
}
renderItem({ title: 'first' });
renderItem({ title: 'second' });
// removing the second row while its update is in flight is exactly the race
list.lastElementChild.remove();The structure at work
The snippet is organized around the function renderItem.
Every asynchronous step is awaited, so rejections surface where the call happens.
What to watch for
- Ready as-is on a bare install of the language.
- A browser is the runtime here:
documentare not available in Node.
Applying it
Signature: async function renderItem(item)
Watch the output for labels — dropped stale update:, applied update: — when the file runs.
Statements that matter
await Promise.resolve(); // any async gap: fetch, timer, user event
if (!row.isConnected) {
console.log('dropped stale update:', item.title);
return;
}
Interactive Tools for This Topic
FAQ
- Does IsConnected Guards Stale Updates run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.