when-dom-ready
Version:
$(document).ready() for the 21st century
44 lines (36 loc) • 980 B
JavaScript
/* eslint no-void: "off" */
// Loaded ready states
var loadedStates = ['interactive', 'complete'];
// Return Promise
var whenDomReady = function whenDomReady(cb, doc) {
return new Promise(function (resolve) {
// Allow doc to be passed in as the lone first param
if (cb && typeof cb !== 'function') {
doc = cb;
cb = null;
}
// Use global document if we don't have one
doc = doc || window.document;
// Handle DOM load
var done = function done() {
return resolve(void (cb && setTimeout(cb)));
};
// Resolve now if DOM has already loaded
// Otherwise wait for DOMContentLoaded
if (loadedStates.indexOf(doc.readyState) !== -1) {
done();
} else {
doc.addEventListener('DOMContentLoaded', done);
}
});
};
// Promise chain helper
whenDomReady.resume = function (doc) {
return function (val) {
return whenDomReady(doc).then(function () {
return val;
});
};
};
export default whenDomReady;
//# sourceMappingURL=index.es2015.js.map