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 🙏

© 2024 – Pkg Stats / Ryan Hefner

document-promises-pinkie

v3.1.5

Published

Document loading states as Promises

Downloads

11

Readme

Document Promises

Document loading states as Promises

NPM Version Build Status Licensing Changelog Gitter Chat

Document Promises is a ponyfill for document.parsed, document.contentLoaded, and document.loaded which allow you to run code after specific states of the document.

fetch('data.json').then(function (data) {
  document.parsed.then(function () {
    document.querySelectorAll('.username').textContent = data.username;
  });
});

document.parsed

document.parsed is a promise that fulfills when the document is parsed and readyState is interactive, before deferred and async scripts have run.

document.contentLoaded

document.contentLoaded is a promise that fulfills when the document is parsed, blocking scripts have completed, and DOMContentLoaded fires.

document.loaded

document.loaded is a promise that fulfills when the document is parsed, blocking scripts have completed, images, scripts, links and sub-frames have finished loading, and readyState is complete.

Usage

Because Document Promises is a ponyfill, it does not attach parsed, contentLoaded or complete to the document by default. Instead, developers are encouraged to import the features individually.

// ES6 example
import { contentLoaded } from 'document-promises';

// CommonJS example
var contentLoaded = require('document-promises').contentLoaded;

Developers may use the ponyfill as-is.

contentLoaded.then(function () {
  /* document is ready */
});

Developers are strongly advised not to attach these promises to document, as the standard may still change substantially, and then such code would be future-incompatible.

FAQ

What’s the difference between these promises and DOMContentLoaded?

One might easily miss an event like DOMContentLoaded if a script fires late, whereas a promise like contentLoaded guarantees the code will execute. Furthermore, using promises for state transitions is much more developer friendly.

What’s the browser support?

Document Promises works all major 2014+ browsers, including Chrome 33+, Edge 12+, Firefox 29+, Opera 20+, Safari 7+, iOS 8+, and Android 4.4.4 & 53+. With Promise and EventListener polyfilled, support goes back to all major 2001+ browsers, including Chrome 1+, Firefox 1+, Internet Explorer 5+, iOS 1+, Netscape Navigator 6+, Opera 7+, Safari 1+, and Android 1+.

What’s the catch?

Document Promises is public domain, dependency free, and 252 bytes or less when minified and gzipped.

Are there known limitations?

Yes, if this polyfill runs in a script that uses defer then contentLoaded will resolve before the DOMContentLoaded event.

Can this be done without a library?

Yes, if something needs to run once the document has reached a certain state, one of the following micro-optimized functions will suffice.

// callback once the document is parsed (119 bytes minified/gzipped)
!function d() {
    /c/.test(document.readyState) && document.body
    ? document.removeEventListener("readystatechange", d) | /* callback */
    : document.addEventListener("readystatechange", d)
}()
// callback once the document is content loaded (120 bytes minified/gzipped)
!function d() {
    /c/.test(document.readyState) && document.body
    ? document.removeEventListener("DOMContentLoaded", d) | /* callback */
    : document.addEventListener("DOMContentLoaded", d)
}()
// callback once the document is fully loaded (112 bytes minified/gzipped)
!function d() {
    /m/.test(document.readyState)
    ? document.removeEventListener("DOMContentLoaded", d) | /* callback */
    : document.addEventListener("DOMContentLoaded", d)
}()

For convenience, each of these callback versions are available as modules.

import onParsed from 'document-promises/callback-versions/onParsed';

onParsed(
  () => {
    // do something on parsed
  }
);