Promisify requestAnimationFrame
If you ever have needed to use the requestAnimationFrame
API in an async
method where you wait for an animation frame before executing additional
logic, the callback nature of it might get annoying. With a super small
wrapper, you can make a function that returns a promise instead!
export function raf(): Promise<DOMHighResTimeStamp> {
return new Promise<DOMHighResTimeStamp>((resolve) => {
requestAnimationFrame(resolve)
})
}
Now, just call await raf()
in your code, and you’re good to go!