“PerformPromiseThen” 在 JavaScript 中的 “throwaway” 承诺是什么承诺?

huangapple go评论73阅读模式
英文:

What is the "throwaway" promise for PerformPromiseThen in JavaScript?

问题

我正在阅读来自V8的这篇文章,该文章介绍了ES2017如何引入了一种用于优化async/await的方法,以减少运行时执行所需的Promise数量。它实际上避免了在等待的值已经是Promise时产生额外的Promise。这也使得可以在错误中收集高效的异步堆栈跟踪

在文章中,它对async函数如何转化为内部操作进行了高级解释。以下是他们在问题中使用的图示:

“PerformPromiseThen” 在 JavaScript 中的 “throwaway” 承诺是什么承诺?

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

然而,我不太理解throwaway promise。它被传递给performPromiseThen操作。然而,PerformPromiseThen的签名是:

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

最后一个可选参数是PromiseCapability Record。我也无法完全理解这个PromiseCapability Record 的概念。它似乎用于异步迭代器,但我不太确定具体的用法。它似乎不是与throwaway promise 所代表的相同事物。所以我的问题是,这个throwaway promise 到底是什么,以及传递给performPromiseThen的参数的语义是什么?

根据我的理解,我可以将它们的内部操作重新编写为基于普通Promise的操作,但throwaway promise 仍然是我无法理解的一个重要部分,无法将其映射到我可以理解的概念中。

英文:

I'm reading this article from V8 about how ES2017 introduced an optimization for async/await to reduce the number of Promises needed in runtime execution. Essentially avoiding an extra Promise when the value being awaited is already a Promise. This also enabled efficient async stack trace to be collected in Errors.

In the article it gave a high-level explanation of how async function translate into internal operations. And here is a figure they used in question:

“PerformPromiseThen” 在 JavaScript 中的 “throwaway” 承诺是什么承诺?

I think the author is using ECMAScript terms for naming the internal operations.

What I don't understand though is the throwaway promise. It's passed to the performPromiseThen operation. However the signature for PerformPromiseThen is:

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

The last optional argument being a PromiseCapability Record. I also can't quite grasp the idea of this PromiseCapability Record. It seems to be used in async iterators but not really sure how. It doesn't seem to be the same thing throwaway promise was representing. So my question is what exactly is this throwaway promise, and what is the semantics of this parameter passed to performPromiseThen?

I can rewrite their internal operations back into plain Promise based on my understanding, but the throwaway promise is still a missing piece that I cannot have a mental mapping to things I can understand.

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);
}      

答案1

得分: 2

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

在规范中,它是一个内部值,用于“封装一个能够解决或拒绝该 promise 的函数”,基本上实现了延迟模式

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是由该方法创建的新promise,用于解决处理程序的结果。这第二个promise就是resultCapability用于的东西。

In other words, you're looking for

换句话说,你正在寻找:

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

其中throwaway值之后不再使用,只是丢弃。请注意,它总是会履行,因此无需未处理的拒绝跟踪。

However the signature for PerformPromiseThen is:

但是,PerformPromiseThen的签名是:

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

The last argument being optional

最后一个参数是可选的。

Indeed, however at the time the article you're quoting was written, the argument was not yet optional.

英文:

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

In the spec, it's an internal value "used to encapsulate a Promise […] along with the functions that are capable of resolving or rejecting that promise.", basically implementing the deferred pattern.

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

Not quite the same, but representing the same thing. In V8, (a reference to) the promise object is all you need to resolve it, just call some of its internal methods. So the code does just directly pass the promise that PerformPromiseThen should resolve.

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

A .then() call deals with two separate promises: in b = a.then(), a is the promise that the handlers are installed on where as b is a new promise created by the method to be resolved with the result of the handlers. This second promise is what the resultCapability is used for.

In other words, you're looking for

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

where the throwaway value is not used afterwards but just thrown away. (Notice it always will fulfill anyway, so no need for unhandled rejection tracking).

> However the signature for PerformPromiseThen is:
>
> PerformPromiseThen( promise, onFulfilled, onRejected [ , resultCapability ] )
>
> The last argument being optional

Indeed, however at the time the article you're quoting was written, the argument was not yet optional.

huangapple
  • 本文由 发表于 2023年6月1日 04:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定