admin管理员组文章数量:1422453
Can someone please explain what this function does? I understand how the requestAnimationFrame
executes before the paint and render within a frame, but I'm not really sure what's the utility of putting it inside a promise.
function nextFrame() {
return new Promise(requestAnimationFrame)
}
Can someone please explain what this function does? I understand how the requestAnimationFrame
executes before the paint and render within a frame, but I'm not really sure what's the utility of putting it inside a promise.
function nextFrame() {
return new Promise(requestAnimationFrame)
}
Share
Improve this question
asked Apr 3, 2020 at 17:39
AdamAdam
1011 silver badge7 bronze badges
1
-
3
The accepted answer gives a great explanation of the mechanics. I think one big motivation would be that you can write
await nextFrame();
inside anasync
function, e.g. at the top of a loop that should run once per animation step. – Matthias Fripp Commented Feb 4, 2021 at 17:31
1 Answer
Reset to default 7Promise constructor accepts a so called executor
function as it's first argument, which can be used to control promise execution flow. This function has two arguments: resolve
and reject
- these are functions you call to either resolve or reject a promise.
const promise = new Promise(function executor(resolve, reject) {
resolve();
// reject();
});
That executor
function is called internally automatically when you create a new promise using new Promise(executor)
.
Next, requestAnimationFrame requires a callback, which will be fired whenever a browser is ready to execute your frame code.
requestAnimationFrame(function frameCallback(timestamp) {
// ...frame code
});
If you try to impose requestAnimationFrame(frameCallback)
signature on top of executor(resolve, reject)
signature, then
requestAnimationFrame
isexecutor
functionframeCallback
isresolve
function
To put it all together, using new Promise(requestAnimationFrame)
- Promise constructor internally calls an executor (in this case
requestAnimationFrame
). AsrequestAnimationFrame
is called, browser internally schedules it as a request for animation. - When browser is ready to animate requested frame, it invokes a callback provided to
requestAnimationFrame
. In our caseresolve
serves as a callback. In additionresolve
will be passed with a timestamp fromrequestAnimationFrame
i.e. the promise will be resolved with a timestamp value.
I guess this code can be used to Promise-ify requestAnimationFrame
, contstructed promise will be resolved as soon as requestAnimationFrame
callback is called.
const promise = new Promise(requestAnimationFrame);
promise.then(function frameCallback(timestamp) => {
// ... frame code
});
本文标签: javascriptrequestAnimationFrame inside a PromiseStack Overflow
版权声明:本文标题:javascript - requestAnimationFrame inside a Promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745369625a2655675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论