I found a couple of questions pretty close to what I am looking for, but either I don’t understand enough or the questions/answers do not exactly apply to what I am looking for:
Consider a very simple example of a requestAnimationFrame loop:
const someList = [];
function main() {
// Start the requestAnimationFrame loop
requestAnimationFrame(update)
}
function update() {
doSomething()
// Loop by calling update again with requestAnimationFrame
requestAnimationFrame(update);
}
function doSomething() {
someList.push(Math.floor(Math.random() * 10));
}
main();
Now here we do not have any problems. We start the loop, do something within the loop, call requestAnimationFrame again and everything is fine.
Now consider this next example. Here we have an async function called doSomethingAsynchronous
and it has to be called with await
. Therefore our update
function needs to be async
as well. My problem is that I do not understand how to work with requestAnimationFrame
when the callback-function passed to the requestAnimationFrame
is asynchronous:
function main() {
// Since update must now be async as well and called with await, what should this line look like?
requestAnimationFrame(update)
}
function async update() {
await doSomething()
// Since update must now be async as well and called with await, what should this line look like?
requestAnimationFrame(update);
}
function async doSomethingAsynchronous() {
// Can't come up with an example, but this function should be called with await
}
main();
I feel like the requestAnimationFrame line in the code should probably look like one of these:
await requestAnimationFrame(update)
await requestAnimationFrame(async () => { await update(); });
requestAnimationFrame(async () => { await update(); });
But I’m not exactly sure if requestAnimationFrame
can or should be awaited. In case I need to supply the update
callback as an async
function, that is also something I can’t wrap my head around.
Edit: In case someone is wondering why I am using requestAnimationFrame
, the reason is that I am using TensorflowJS Object Detection model which returns the detection results asynchronously. So I want to asynchronously get the results and then render and visualize the results of a canvas – thus the requestAnimationFrame
.
2
Answers
If you want to make async rendering:
I don’t see any problem with the code you propose yourself, but as you have not included the rendering code, make sure that it is executed at the top of your
update
function, so that onceupdate
is called, it is executed synchronously.I would however suggest to promisify
requestAnimationFrame
like this:This looks cryptic, but it is short for:
…but without creating the arrow function wrapper.
And then you can write an async
while
loop where you await both your own promise and thenextFrame
one: