Javascript 源代码中关于注释的问题的v8闭包
我只是在看v8编译器,特别是promise-all-element-closure.tq中的那一行。我遇到了这些行Javascript 源代码中关于注释的问题的v8闭包,javascript,compilation,v8,Javascript,Compilation,V8,我只是在看v8编译器,特别是promise-all-element-closure.tq中的那一行。我遇到了这些行 // Promise.allSettled, for each input element, has both a resolve and a // reject closure that share an [[AlreadyCalled]] boolean. That is, the // input element can only be settled
// Promise.allSettled, for each input element, has both a resolve and a
// reject closure that share an [[AlreadyCalled]] boolean. That is, the
// input element can only be settled once: after resolve is called, reject
// returns early, and vice versa. Using {function}'s context as the marker
// only tracks per-closure instead of per-element. When the second
// resolve/reject closure is called on the same index, values.object[index]
// will already exist and will not be the hole value. In that case, return
// early. Everything up to this point is not yet observable to user code.
// This is not a problem for Promise.all since Promise.all has a single
// resolve closure (no reject) per element.
这就像一个错误修复。我不太明白Promise.allSettled怎么可能对同一元素多次调用resolve reject?出了什么问题
我不太明白Promise.allSettled怎么可能对同一元素多次调用resolve reject
Promise.allSettled
根本不给他们打电话。创建和解决承诺的代码确实如此,并且可以多次调用它们。因此,Promise.allsolited
只需通过从您在中看到注释的函数中提前返回来处理这种情况。如果它在该索引上没有看到承诺的解决方案,那么它将继续,以便能够在它填充的数组中填充how
下面是一个函数的示例,其中a)调用拒绝
一次,或B)调用解析
至少一次(可能不止一次),还调用拒绝
:
function findMatchInStream(stream, text) {
return new Promise((resolve, reject) => {
stream.on("line", line => {
if (line.includes(text)) {
resolve(line);
}
});
stream.on("end", () => reject());
});
}
该代码通过检查行中的给定文本来响应Node.js-likeline
事件,如果找到该行,则调用resolve
——即使之前已经调用了resolve
。类似地,当它到达流的末尾时,它调用reject
,即使之前调用了resolve
。这没关系(尽管在我看来,可怜的),因为一旦你实现了承诺,你就不能再实现它;对resolve
或reject
的后续调用将被忽略
不幸的是,我在野外看到过这样的代码
我不太明白Promise.allSettled怎么可能对同一元素多次调用resolve reject
Promise.allSettled
根本不给他们打电话。创建和解决承诺的代码确实如此,并且可以多次调用它们。因此,Promise.allsolited
只需通过从您在中看到注释的函数中提前返回来处理这种情况。如果它在该索引上没有看到承诺的解决方案,那么它将继续,以便能够在它填充的数组中填充how
下面是一个函数的示例,其中a)调用拒绝
一次,或B)调用解析
至少一次(可能不止一次),还调用拒绝
:
function findMatchInStream(stream, text) {
return new Promise((resolve, reject) => {
stream.on("line", line => {
if (line.includes(text)) {
resolve(line);
}
});
stream.on("end", () => reject());
});
}
该代码通过检查行中的给定文本来响应Node.js-likeline
事件,如果找到该行,则调用resolve
——即使之前已经调用了resolve
。类似地,当它到达流的末尾时,它调用reject
,即使之前调用了resolve
。这没关系(尽管在我看来,可怜的),因为一旦你实现了承诺,你就不能再实现它;对resolve
或reject
的后续调用将被忽略
可悲的是,我在野外看到过类似这样的代码。指向您在阅读中发现此评论的源代码的链接将有助于上下文。指向您在阅读中发现此评论的源代码的链接将有助于上下文。这会使Thank肯定对承诺所做的事感到困惑。这会使谢天谢地,我完全搞不清楚承诺在做什么。