Skip to main content
100%

Wait for an element to exist. ES6, Promise, MutationObserver

✓ Published0🌍 Public
Aanemochore
Last edited Oct 26, 2020
Created on Oct 26, 2020

This example demonstrates a utility for resolving race conditions in web applications by waiting until an element matching a given selector exists in the DOM, then returning a promise that resolves with that element. The code uses native ES6 Promises and the MutationObserver API to monitor the document’s root element for child list and subtree changes. When the target element appears, the observer disconnects and the promise resolves, allowing actions like removing the widget. The accompanying usage example shows how to delete `#someWidget` once it is present.

AI-generated description

Similar vizzes