This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-10-03 09:15:47 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {(() => void) | (() => Promise<void>)} callback
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
export default function ready(callback) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function loaded() {
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = callback();
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof result?.then === 'function') {
|
|
|
|
result.then(resolve).catch(reject);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['interactive', 'complete'].includes(document.readyState)) {
|
|
|
|
loaded();
|
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', loaded);
|
|
|
|
}
|
|
|
|
});
|
2017-07-14 02:08:56 -07:00
|
|
|
}
|