Skip to content

Instantly share code, notes, and snippets.

@pazguille
Last active October 19, 2023 18:11
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 pazguille/6ade140e7b8b7d84812df64b18e4d584 to your computer and use it in GitHub Desktop.
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.
// Based on: https://web.dev/optimize-long-tasks/
window.yieldToMain = function yieldToMain(task) {
return new Promise(resolve => {
setTimeout(() => {
task && task();
resolve();
}, 0);
});
}
// await yieldToMain(...);
@ShinobiWPS
Copy link

ShinobiWPS commented Oct 19, 2023

I was wondering, wouldn't be queueMicrotask of help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment