Last active
October 19, 2023 18:11
-
-
Save pazguille/6ade140e7b8b7d84812df64b18e4d584 to your computer and use it in GitHub Desktop.
Break up your long tasks. One way to yield to the main thread using a combination of a Promise that resolves with a call to setTimeout.
This file contains 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
// Based on: https://web.dev/optimize-long-tasks/ | |
window.yieldToMain = function yieldToMain(task) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
task && task(); | |
resolve(); | |
}, 0); | |
}); | |
} | |
// await yieldToMain(...); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was wondering, wouldn't be
queueMicrotask
of help?