简体   繁体   English

Promise 内的 requestAnimationFrame

[英]requestAnimationFrame inside a Promise

Can someone please explain what this function does?有人可以解释一下这个 function 的作用吗? 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.我了解requestAnimationFrame在绘制和渲染帧之前是如何执行的,但我不确定将它放入 promise 有什么用处。

function nextFrame() {
  return new Promise(requestAnimationFrame)
}

Promise constructor accepts a so called executor function as it's first argument, which can be used to control promise execution flow. Promise构造函数接受所谓的executor器 function 作为它的第一个参数,可用于控制 promise 执行流程。 This function has two arguments: resolve and reject - these are functions you call to either resolve or reject a promise.这个 function 有两个 arguments: resolvereject - 您调用这些函数来解决或拒绝 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) .当您使用new Promise(executor)创建新的 promise 时,该executor程序 function 会在内部自动调用。

Next, requestAnimationFrame requires a callback, which will be fired whenever a browser is ready to execute your frame code.接下来, requestAnimationFrame需要一个回调,只要浏览器准备好执行您的帧代码,就会触发该回调。

requestAnimationFrame(function frameCallback(timestamp) {
    // ...frame code
});

If you try to impose requestAnimationFrame(frameCallback) signature on top of executor(resolve, reject) signature, then如果您尝试在executor(resolve, reject)签名之上施加requestAnimationFrame(frameCallback)签名,那么

  • requestAnimationFrame is executor function requestAnimationFrameexecutor function
  • frameCallback is resolve function frameCallbackresolve function

To put it all together, using new Promise(requestAnimationFrame)总而言之,使用new Promise(requestAnimationFrame)

  1. Promise constructor internally calls an executor (in this case requestAnimationFrame ). Promise 构造函数在内部调用执行器(在本例中为requestAnimationFrame )。 As requestAnimationFrame is called, browser internally schedules it as a request for animation.requestAnimationFrame被调用时,浏览器在内部将其调度为对 animation 的请求。
  2. When browser is ready to animate requested frame, it invokes a callback provided to requestAnimationFrame .当浏览器准备好为请求的帧设置动画时,它会调用提供给requestAnimationFrame的回调。 In our case resolve serves as a callback.在我们的例子中, resolve用作回调。 In addition resolve will be passed with a timestamp from requestAnimationFrame ie the promise will be resolved with a timestamp value.此外, resolve将使用requestAnimationFrame的时间戳传递,即 promise 将使用时间戳值解析。

I guess this code can be used to Promise-ify requestAnimationFrame , contstructed promise will be resolved as soon as requestAnimationFrame callback is called.我想这段代码可以用来 Promise-ify requestAnimationFrame ,构造的 promise 将在requestAnimationFrame回调被调用时被解析。

const promise = new Promise(requestAnimationFrame);
promise.then(function frameCallback(timestamp) => {
  // ... frame code
});

...or simply: ...或者简单地说:

await new Promise(requestAnimationFrame);
// <-- body of the former frameCallback function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM