Skip to content

Instantly share code, notes, and snippets.

@webdesignberlin
Forked from potch/awaitIdle.js
Created December 16, 2016 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdesignberlin/fc16d42d8cfbdbf4a982c2e1606b3a28 to your computer and use it in GitHub Desktop.
Save webdesignberlin/fc16d42d8cfbdbf4a982c2e1606b3a28 to your computer and use it in GitHub Desktop.
Rather rough sketch of using requestIdleCallback to break up JS execution
function idle(action) {
return new Promise((resolve, reject) => {
requestIdleCallback(timing => resolve(action(timing)));
});
}
async function init() {
criticalPath();
await idle(lessImportant);
await idle(nonCritical);
}
function criticalPath() {
console.log('lots of important stuff');
}
function lessImportant() {
console.log('less important stuff');
}
function nonCritical() {
console.log('non-important stuff');
}
window.addEventListener('load', init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment