【问题标题】:Create a promise here, resolve it there在这里创建一个承诺,在那里解决它
【发布时间】:2022-02-16 00:59:58
【问题描述】:

我以为我在所有情况下都掌握了承诺,但我被困在这里:我希望在计时器上发生动画。我可以从图形包中获得的唯一信息是帧号,因此我根据帧速率估计来计算结束帧。

图形包是p5.js,它公开了frameCount,它在每个绘制周期仅以大约60 fps 的速度运行。 (我在这个问题中将其命名为 currentFrame,希望能澄清)。据我所知,这是我在 p5.js 中进入动画帧状态的唯一可见性

doAnimation(duration) {
  // animation stuff
  this.endFrame = currentFrame + (60 * duration); // 60fps seems to be a good guess
}

draw() {
  if (currentFrame < this.endFrame) {
    // compute and draw the state of the animation
  } else if (currentFrame >= this.endFrame) {
    // animation done
  }
}

我很困惑的是如何给 doAnimation 调用者一个在其他方法中解析的承诺。我试过这个:

doAnimation(duration) {
  // animation stuff
  this.endFrame = currentFrame + (60 * duration);
  this.animationPromise = new Promise(resolve => {
    // I realize this is wrong, but it illustrates the problem. How do I run this test later?
    if (currentFrame >= this.endFrame) resolve();
  });
  return this.animationPromise;
}

draw() {
  if (currentFrame < this.endFrame) {
    // compute and draw the state of the animation
  } else if (currentFrame >= this.endFrame) {
    this.animationPromise.resolve();  // also wrong, I think
    // since resolve isn't an instance method of a promise
  }
}

有人能帮我搞清楚这个吗?

【问题讨论】:

  • currentFrame 是某个东西的参数吗? draw的?还是真的是全球性的?
  • @trincot - 图形包将其公开为全局
  • 承诺的目的是什么?在做某事之前等待承诺的是什么?
  • @T.J.Crowder - 调用者想要在这个动画完成后开始另一个动画。所以来电者想说object.doAnimation(2).then( // do another
  • 我会为这个问题添加更多说明。

标签: javascript promise


【解决方案1】:

您需要存储要调用的解析器,而不是存储要解析的承诺:

doAnimation(duration) {
  // animation stuff
  this.endFrame = currentFrame + (60 * duration);
  return new Promise(resolve => {
    this.onAnimationFinished = resolve;
  });
}

draw() {
  if (currentFrame < this.endFrame) {
    // compute and draw the state of the animation
  } else if (currentFrame >= this.endFrame) {
    this.onAnimationFinished();
  }
}

当然,您需要确保一次只调用一次doAnimation(),并且在doAnimation() 之前不会调用draw()(或者更准确地说,当@ 时设置onAnimationFinished 987654326@ 已设置)。一旦到达结束帧,您可能还想将它们重置(到undefined)。

【讨论】:

  • 太棒了!太感谢了!如果我需要同时进行更多操作,我可以保留一组解析器,对吗?
  • 是的,你可以像优先队列一样 - 每个项目都是框架和解析器的组合,draw() 总是查看队列的头部。
  • 嘿,看来我的答案不再需要了。保持队列优先顺序的好处是,尽管您必须有 大量 活动动画才能真正发挥作用。
  • @T.J.Crowder 是的,实际上我们应该能够假设active 集合中的每个动画都会在每一帧中进行一些绘制,因此检查动画是否完成并没有任何开销。但我不知道 p5.js 如何(或是否)管理这个,以及 OP 打算使用什么样的对象模型。
【解决方案2】:

一般来说,履行或拒绝一个承诺应该由你传递给new Promise的执行器函数中启动的进程来完成。很少需要从外部履行或拒绝承诺(至少,以调用者意识到这是承诺履行的方式)。

我会使用回调队列:

  1. 维护一个待处理动画列表,其中包含它们结束的帧和一个回调。

  2. 让draw 根据(新的)currentFrame 查看应该调用的动画回调队列。

  3. 让doAnimation 代码排队回调

大致:

activeAnimations = [];

doAnimation(duration) {
    // animation stuff
    const endFrame = currentFrame + (60 * duration); // 60fps seems to be a good guess
    return new Promise(resolve => {
        this.activeAnimations.push({
            endFrame,
            done: resolve, // Or possibly: `done: () => resolve()`
                           // so this code is in control of whether
                           // there's a fulfillment value
        });
    });
}

draw() {
    for (let index = 0; index < this.activeAnimations; ++index) {
        const animation = this.activeAnimations[index];
        if (currentFrame > animation.endFrame) {
            // Animation done
            animation.done();
            this.activeAnimations.splice(index, 1);
            --index;
        } else {
            // Continue animation
        }
    }
}

done: () =&gt; resolve() 注释的原因是,一般来说,我尽量避免将 Promise resolve/reject 函数直接暴露给 Promise 执行器之外的代码,因为它之外的代码通常不应该直接控制 promise 发生的事情。 (我对setTimeout 例外。:-))但这可能有点矫枉过正。

【讨论】:

  • 我明白了,非常感谢! @bergi 关于运行多个的评论的设计。如果我最终需要多个,将使用它。现在,它们连续的动画发生在这个对象的不同实例上。
  • @goodson - 有点,我没有想到 Bergi 将其设为优先队列。我想知道这是否只是重复了他的答案(我在发布时看到了他的答案)。
【解决方案3】:

作为替代方案,您也可以在 Promise 构造函数回调中分配给draw:

function basicDraw() {
    // ...
}
var draw = basicDraw; // Default function

doAnimation(duration) {
    // animation stuff
    this.endFrame = currentFrame + (60 * duration);
    return new Promise((resolve) => {
        draw = () => {
            basicDraw();
            if (currentFrame < this.endFrame) {
                // compute and draw the state of the animation
            } else if (currentFrame >= this.endFrame) {
                draw = basicDraw; // back to normal
                resolve();
            }
        };
    });
}

doAnimation(1000).then(() => {
     /* whatever you want to do next */ 
});

【讨论】:

  • 这个很有创意。谢谢!
猜你喜欢
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多