Document Promises is a polyfill for document.interactive, document.contentLoaded, and document.loaded which allow you to run code after specific states of the document.
fetch('data.json').then(function (data) {
document.interactive.then(function () {
document.querySelectorAll('.username').textContent = data.username;
});
});
document.interactive is a promise that fulfills when the when the document's readyState
becomes interactive
.
document.contentLoaded is a promise that fulfills when the DOMContentLoaded
event fires on the document.
document.loaded is a promise that fulfills when the when the document's readyState
becomes complete
.
Using promises for state transitions is much more developer friendly.
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+.
Document Promises is public domain, dependency free, and 195 bytes when minified and gzipped.