Created
December 15, 2016 19:21
-
-
Save potch/426688ed38acd2d10c4a638a49f23505 to your computer and use it in GitHub Desktop.
Rather rough sketch of using requestIdleCallback to break up JS execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Oh, I never thought about it this way, I always envisioned an approach more like this (the loop is just there for illustration):