
Product
Introducing Custom Tabs for Org Alerts
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.
dom-mutations
Advanced tools
Observe changes to the DOM using an async iterable — A nicer API for MutationObserver
Observe changes to the DOM using an async iterable — A nicer API for MutationObserver
This package only works in the browser.
npm install dom-mutations
import domMutations from 'dom-mutations';
const target = document.querySelector('#unicorn');
for await (const mutation of domMutations(target, {childList: true})) {
console.log('Mutation:', mutation);
}
Accepts the same arguments as MutationObserver#observe() with an additional optional signal option to abort the observation. If the signal is triggered, the async iterable throws an abort error.
Returns an AsyncIterable that yields MutationRecord objects representing individual mutations.
Similar to domMutations(), but yields batches of MutationRecord objects, each batch representing a group of mutations captured together. This method is less convenient, but can be useful in some cases when you need to handle mutations together as a group.
import {batchedDomMutations} from 'dom-mutations';
const target = document.querySelector('#unicorn');
for await (const mutations of batchedDomMutations(target, {childList: true})) {
console.log('Batch of mutations:', mutations);
}
Simply return or break in the loop body.
Triggering the iterator to return
import domMutations from 'dom-mutations';
const target = document.querySelector('#unicorn');
const mutationIterator = domMutations(target, {childList: true})[Symbol.asyncIterator]();
(async () => {
for await (const mutation of mutationIterator) {
console.log('Mutation:', mutation);
}
})();
setTimeout(() => {
mutationIterator.return();
}, 10000);
Using a variable
This has the downside of not ending the iteration until the next mutation.
import domMutations from 'dom-mutations';
const target = document.querySelector('#unicorn');
let shouldStop = false;
(async () => {
for await (const mutation of domMutations(target, {childList: true})) {
if (shouldStop) {
break;
}
console.log('Mutation:', mutation);
}
})();
setTimeout(() => {
shouldStop = true;
}, 10000);
Using AbortController
Unlike the above approaches, this will make the iterable throw an abort error.
import domMutations from 'dom-mutations';
const target = document.querySelector('#unicorn');
const controller = new AbortController();
const {signal} = controller;
(async () => {
for await (const mutation of domMutations(target, {childList: true, signal})) {
console.log('Mutation:', mutation);
}
})();
setTimeout(() => {
controller.abort();
}, 10000);
requestAnimationFrame as an async iterable, in any JavaScript environmentFAQs
Observe changes to the DOM using an async iterable — A nicer API for MutationObserver
The npm package dom-mutations receives a total of 711 weekly downloads. As such, dom-mutations popularity was classified as not popular.
We found that dom-mutations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.