javascript - JavaScript 中 PerformPromiseThen 的 "throwaway"Promise 是什么?

标签 javascript asynchronous promise v8

我正在阅读this article from V8关于 ES2017 如何引入 async/await 的优化来减少运行时执行所需的 Promise 数量。当等待的值已经是一个 Promise 时,本质上是避免额外的 Promise。这也启用了 efficient async stack trace将收集在错误中。

在文章中,它对如何 async function translate into internal operations 进行了高级解释。这是他们在问题中使用的一个数字:

我认为作者使用 ECMAScript 术语来命名内部操作。

但我不明白的是一次性 promise 。它被传递给 performPromiseThen 操作。然而 PerformPromiseThen 的签名是:

PerformPromiseThen(promise, onFulfilled, onRejected [, resultCapability])

最后一个可选参数是 PromiseCapability Record 。我也不太明白这个 PromiseCapability Record 的想法。它似乎在异步迭代器中使用,但不太确定如何使用。这似乎与 Throwaway Promise 所代表的东西不同。所以我的问题是这个 Throwaway Promise 到底是什么,传递给performPromiseThen 的这个参数的语义是什么?

我可以根据我的理解将它们的内部操作重写回普通的 Promise,但是一次性 Promise 仍然是一个缺失的部分,我无法在心理上映射到我可以理解的事物。

function foo(v) {                                 // resumable function foo(v) {
  const implicit_promise = new Promise            //   implicit_promise = createPromise();
  ((resolvePromise, _throw) => {
                                                  //   // 1. wrap v in a promise
    const promise = new Promise(resolvePromise => //   promise = createPromise();
      resolvePromise(/* promise, */ v));          //   resolvePromise(promise, v);

                                                  //   // 2. attach handlers for resuming foo
    /* throwaway = createPromise(); */            //   throwaway = createPromise();
    promise.then(                                 //   performPromiseThen(promise,
      res => resume(/* «foo», */ res),            //     res => resume(«foo», res),
      err => _throw(/* «foo», */ err),            //     err => throw(«foo», err),
      /* throwaway */);                           //     throwaway);

    function resume(/* «foo», */ res) {           //   // 3.a suspend foo ...
      w = /* resumed */ res;                      //   w = suspend(«foo», ...);
      resolvePromise(/* implicit_promise, */ w);  //   resolvePromise(implicit_promise, w);
    }
  });                                             //   // 3.b ... and return implicit_promise
  return implicit_promise;                        //   suspend(..., implicit_promise);
}      

最佳答案

I also can't quite grasp the idea of this PromiseCapability Record.

在规范中,它是一个内部值“用于封装 Promise [...] 以及能够解析或拒绝该 Promise 的函数。”,基本上实现了deferred pattern .

It doesn't seem to be the same thing throwaway promise was representing.

不完全相同,但代表相同的事物。在 V8 中,您只需调用 Promise 对象(对它的引用)即可解析它,只需调用它的一些内部方法即可。因此,代码只是直接传递 PerformPromiseThen 应该解析的 promise 。

What exactly is this throwaway promise, and what is the semantics of this parameter passed to performPromiseThen?

.then() 调用处理两个单独的 Promise:在 b = a.then() 中,a 是 Promise处理程序安装在 b 上,因为 b 是由要使用处理程序的结果解析的方法创建的新 Promise。第二个 promise 就是 resultCapability 的用途。

换句话说,您正在寻找

const throwaway = promise.then(
    res => _resume(/* «foo», */ res),
    err => _throw(/* «foo», */ err),
);

其中 throtaway 值之后不再使用,而是被丢弃。 (请注意,无论如何它总是会满足,因此不需要未处理的拒绝跟踪)。

However the signature for PerformPromiseThen is:

PerformPromiseThen( promise, onFulfilled, onRejected [ , resultCapability ] )

The last argument being optional

确实如此,但是在您引用的文章撰写时,the argument was not yet optional .

关于javascript - JavaScript 中 PerformPromiseThen 的 "throwaway"Promise 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76376921/

相关文章:

javascript - 处理多次射击

javascript - 查询具有多个参数的 JavaScript 对象

javascript - 异步/等待函数返回未定义?

javascript - 如何在 Javascript 中强制同步循环执行?

javascript - 无法使用resolve(arg) Javascript/Odoo 获取Ajax 结果

node.js - 是否有替代已弃用的 deferred.callback 的替代品?

javascript - 为什么 Jquery 代码被多次触发

javascript - 原型(prototype)继承中的 super

javascript - node.js 异步/等待与 MySQL 一起使用

javascript - CoffeeScript/JavaScript - promise 与 Source Maps 兼容?