Carlos Dubón
BlogCreatingContactUsesResume
HomeBlogCreatingContactUsesResume

Apr 12, 2024

yieldToMain

Manually defer code execution by briefly interrupting your work to give the browser opportunities to run more important tasks.

// https://web.dev/articles/optimize-long-tasks#defer-code-execution
export const yieldToMain = async (callback: () => void) => {
  if (globalThis?.scheduler?.yield) {
    await globalThis.scheduler.yield();
  } else {
    await new Promise((resolve) => setTimeout(resolve, 0));
  }
 
  callback();
};

Enjoy my content? Stay updated with my newsletter.

No spam, just useful updates. Read privacy policy.
Carlos Dubón
Carlos Dubón
Software Engineer
© 2024-present Carlos Dubón. All Rights Reserved.

General

  • Home
  • Creating
  • Contact
  • Resume

Writing

  • Blog
  • Snippets
  • Uses

What I’m listening to

Not playing
Spotify