npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

get-element-promise-by-id

v0.1.4

Published

An implementation of getElementById that returns a promise that waits to resolve when the element will be added to the DOM. Using mutation observer and cancelable with an abort signal or a timeout

Readme

get-element-promise-by-id

An implementation of getElementById that returns a promise and waits for the element to be added to the DOM if it's not already present. Supports timeouts and AbortController signals for flexible async DOM element handling.

npm version License: MIT

Features

  • Promise-based: Clean async/await syntax
  • Timeout support: Set custom timeouts or wait indefinitely
  • Abortable: Cancel operations using AbortController
  • MutationObserver: Efficiently watches for DOM changes
  • TypeScript: Full TypeScript support with type definitions
  • Lightweight: Minimal dependencies and small bundle size

Installation

npm install get-element-promise-by-id

Usage

Basic Usage

import { getElementPromiseById } from 'get-element-promise-by-id';

// Wait for an element to appear
const element = await getElementPromiseById('my-element');
console.log('Element found:', element);

With Timeout

// Wait up to 5 seconds for the element
try {
  const element = await getElementPromiseById('my-element', { timeout: 5000 });
  console.log('Element found:', element);
} catch (error) {
  if (error.name === 'TimeoutError') {
    console.log('Element not found within timeout');
  }
}

With AbortController

const controller = new AbortController();

// Start waiting for element
const promise = getElementPromiseById('my-element', { 
  signal: controller.signal 
});

// Cancel after 3 seconds
setTimeout(() => controller.abort(), 3000);

try {
  const element = await promise;
  console.log('Element found:', element);
} catch (error) {
  if (error.name === 'AbortError') {
    console.log('Operation was cancelled');
  }
}

API

getElementPromiseById(id, options?)

Returns a promise that resolves when an element with the specified ID is found in the DOM.

Parameters

  • id (string): The ID of the element to wait for
  • options (object, optional):
    • timeout (number, default: 0): Timeout in milliseconds. Set to 0 for no timeout
    • signal (AbortSignal): AbortSignal to cancel the operation

Returns

  • Promise<HTMLElement>: Promise that resolves with the found element

Throws

  • DOMException with name 'TimeoutError': When the timeout is reached
  • DOMException with name 'AbortError': When the operation is aborted

Demo

Try the interactive demo at: https://tombigel.github.io/get-element-promise-by-id/

Or run it locally:

git clone https://github.com/tombigel/get-element-promise-by-id.git
cd get-element-promise-by-id
npm install
npm run dev

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Run with coverage
npm run coverage

# Build library
npm run build:lib

# Build demo
npm run build:demo

# Start development server
npm run dev

Browser Support

This library uses modern JavaScript features including:

  • MutationObserver
  • AbortController
  • Promises

Supported in all modern browsers. For older browser support, ensure these APIs are polyfilled.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Tom Bigelajzen
@tombigel