← Back to1 LOC

Wait for the next event loop

Written byPhuoc Nguyen
Created
14 Dec, 2023
Category
Misc
Tags
event loop, next tick, requestAnimationFrame, setTimeout(0)
In JavaScript, there may be times when you need to wait for the next event loop before executing a block of code. For example, you might want to ensure that certain updates to the DOM have been completed before running a function.
To do this, you can create a `Promise` that resolves after the next event loop. The following `waitForNextLoop()` function uses `setTimeout` with a delay of `0` to queue a function call on the event loop. When this function is called, it resolves the `Promise`. This `Promise` can be used in other parts of your code to ensure that the next event loop has completed before proceeding.
waitForNextLoop.js
const waitForNextLoop = async () => new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 0);
});
Here's an example of how you can use the `waitForNextLoop()` function:
js
waitForNextLoop().then(() => {
console.log('This code runs after the next event loop!');
});
By using `requestAnimationFrame` instead of `setTimeout`, we can provide some performance benefits. This is because `requestAnimationFrame` allows the browser to optimize when to run your code. It also ensures that your code runs at a consistent frame rate, which can help prevent jank and stuttering in animations.
Here's the modified version:
nextTick.ts
const nextTick = (): Promise<void> => new Promise((resolve) => requestAnimationFrame(() => resolve()));
When you call `nextTick()`, it returns a new `Promise` that resolves when the next animation frame is ready to be rendered. This ensures that any updates to the UI or animations will be finished before the `Promise` resolves.
Here's an example of how to use the `nextTick()` function:
js
async function doSomethingAsync() {
await nextTick();
// Code here will run after the next animation frame
}

doSomethingAsync();

See also

If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn