Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active October 23, 2018 17:22
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 AutoSponge/8e971d482a78030dff1213a4614a3698 to your computer and use it in GitHub Desktop.
Save AutoSponge/8e971d482a78030dff1213a4614a3698 to your computer and use it in GitHub Desktop.
idle until urgent
const IDLE = Symbol('IDLE')
const idle = fn => {
let result = IDLE
const resolve = requestIdleCallback(() => {
result = fn()
})
return () => {
if (result === IDLE) {
cancelIdleCallback(resolve)
result = fn()
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment