是否“解析"?始终表示与&“完成& amp; amp; amp; amp; amp; amp; amp;"有所不同? [英] Does "resolve" consistently mean something distinct from "fulfill"?

查看:79
本文介绍了是否“解析"?始终表示与&“完成& amp; amp; amp; amp; amp; amp; amp;"有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(相关但不完全相同: JS承诺:实现与解决)

(Related but not quite the same: JS Promises: Fulfill vs Resolve)

我一直在努力围绕Javascript Promise,我一直在努力解决 resolve resolved 的基本概念,而不是 fulfill已实现.我已经阅读了几篇介绍,例如杰克·阿奇博尔德的以及浏览一些

I've been trying to wrap my head around Javascript promises, and I'm struggling with the basic notions of resolve and resolved, vs. fulfill and fulfilled. I have read several introductions, such as Jake Archibald's, as well as browsing some relevant specs.

状态与命运(不是正式规范,而是由规范作者之一撰写的权威文档),已实现是一种状态,而 resolved 是一种命运"(无论如何)那是-但它们显然是截然不同的):

In States and Fates (not quite an official spec, but referenced as an authoritative document written by one of the spec authors), fulfilled is a state while resolved is a "fate" (whatever that is -- but they're clearly distinct):

承诺具有三种可能的互斥状态:已实现,已拒绝,仍待处理.

Promises have three possible mutually exclusive states: fulfilled, rejected, and pending.

  • 如果 promise.then(f)会尽快调用f,则将实现承诺可能."
  • A promise is fulfilled if promise.then(f) will call f "as soon as possible."

如果承诺解决拒绝没有任何效果,则已解决也就是说,诺言已锁定"以遵循另一个诺言承诺,或者已已实现或已被拒绝

A promise is resolved if trying to resolve or reject it has no effect, i.e. the promise has been "locked in" to either follow another promise, or has been fulfilled or rejected

特别是,已解决既包含已实现,又包含已被拒绝(并且已锁定).与 reject 相对的(或直接对应的功能)是 fulfill ,而不是 resolve resolve 包括 reject 作为其可能性之一.

In particular, resolved encompasses both fulfilled and rejected (and locked in). The "opposite" (or directly corresponding function) to reject is fulfill, not resolve; resolve includes reject as one of its possibilities.

规范指的是同时使用 fulfill resolve then()方法(或其对应的抽象概念)的第一个参数:

Yet the spec refers to the first argument to the then() method (or its corresponding abstract concepts) using both fulfill and resolve:

25.4:如果p.then( f ,r)立即将Job放入队列以调用函数f.

25.4: A promise p is fulfilled if p.then(f, r) will immediately enqueue a Job to call the function f.

  • 25.4.1.1:[[解决]]函数对象用于解决给定的Promise对象的函数.

    25.4.1.1: [[Resolve]] A function object The function that is used to resolve the given promise object.

  • 25.4.1.3:将 resolve 的[[Promise]]内部插槽设置为promise.将解决的[[AlreadyResolved]]内部插槽设置为已经解决.[然后,以完全对应的方式使用 reject .]

    25.4.1.3: Set the [[Promise]] internal slot of resolve to promise. Set the [[AlreadyResolved]] internal slot of resolve to alreadyResolved. [Then immediately after, reject is used in an exactly corresponding way.]

  • 25.4.5.3:Promise.prototype.then( onFulfilled ,onRejected

    25.4.5.3: Promise.prototype.then ( onFulfilled , onRejected )

  • 也许最关键的之一是

    25.4.4.5答应.解决(x)

    其中 MDN 描述的如下:

    Promise.resolve(value)方法返回使用给定值解析的Promise对象.如果该值是一个thenable(即具有"then"方法),则返回的Promise将跟随"该thenable,并采用其最终状态;否则,返回的承诺将被满足的值.

    The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

    没有提到 Promise.resolve()方法有可能拒绝.此外,还有一个 Promise.reject()方法,但没有 Promise.fulfill()方法.因此,在这里,与 reject 相对应的是 resolve ,而不是 fulfill .

    There is no mention of the Promise.resolve() method having the potential to reject. Furthermore, there is a Promise.reject() method, but no Promise.fulfill() method. So here, the counterpart to reject is resolve, instead of fulfill.

    当然,"fate"一词 resolved 和方法(或动词) resolve 之间可能没有保证的相关性.但是,当术语已实现时,让 resolve()将承诺放入明确的已实现状态真的是令人误解和困惑.>和 resolved 已被仔细定义为具有不同的含义.

    Granted, there might be no guaranteed correlation between the "fate" term resolved, and the method (or verb) resolve. But it would be (is?) really misleading and confusing to have resolve() put a promise into a specifically fulfilled state, when the terms fulfilled and resolved have carefully been defined to have distinct meanings.

    这是这里发生的事情吗?右手不知道左手在做什么,而我们最终得出的文件被认为是使用不一致的术语来作为机芯指引的文件?还是我缺少某些东西,而 resolve 实际上是比 fulfill 更合适的术语,用于 resolve()方法的作用?

    Is that what has happened here... the right hand didn't know what the left hand was doing, and we ended up with the documents that are supposed to be the guiding lights of the movement using terms in inconsistent ways? Or is there something I'm missing, and resolve is actually a more appropriate term than fulfill for what the resolve() method does?

    我并不是要批评文档作者.我了解在历史悠久的广泛分布的小组中,如何使所有术语在所有文档中一致使用是很困难的.在深入研究这些文档的术语和措辞时,我的目的是准确理解术语-包括了解 fulfill resolve 之类的术语精确度的局限性可以真正区分.杰克·阿奇博尔德(Jake Archibald)承认,他有时会术语混在一起.对于像我这样试图理解术语的新手来说,这是非常有用的录入!杰克,谢谢你的脆弱.:-)我问这个问题的目的是找出这些术语的哪些定义或用法是可靠的?还是我应该得出结论,即使在最权威的文档中,有时也会专门使用 resolve 来表示 fulfill ,有时甚至是 fulfill/reject/lock in ?

    I don't mean to come across as critical of the document authors. I understand how, with a widely-distributed group, over history, it's difficult to get all the terms to be used consistently across all documents. My purpose here, in digging into the terminology and phrasing of these documents, is to understand the terms accurately -- which includes knowing the limits of how precisely terms like fulfill and resolve can really be distinguished. Jake Archibald admits that he sometimes gets the terms mixed up. That's a very helpful admission to noobs like me who are trying to make sense of terminology! Thank you, Jake, for being vulnerable. :-) My purpose in asking this question is to find out, which definitions or usages of those terms are reliable? Or should I conclude that resolve is sometimes used specifically to mean fulfill, and sometimes fulfill/reject/lock in, even in the most authoritative documents?

    推荐答案

    我正在努力解决 resolve resolved 的基本概念,而不是 fulfill fulfilled .

    I'm struggling with the basic notions of resolve and resolved, vs. fulfill and fulfilled.

    看看什么是JavaScript Promise的正确术语.是的,它们有时会混在一起,但让我们试着忽略它.

    Have a look at What is the correct terminology for javascript promises. Yes, they're sometimes mixed up, but let's try to ignore that.

    然而,规范使用 fulfill resolve 引用了 then()方法(或其对应的抽象概念)的第一个参数.我们是否最终以不一致的方式使用术语来作为本应作为运动指导的文件?

    Yet the spec refers to the first argument to the then() method (or its corresponding abstract concepts) using both fulfill and resolve. Did we end up with the documents that are supposed to be the guiding lights of the movement using terms in inconsistent ways?

    不,这里没有矛盾,规范中的术语是准确的.

    No, there is no inconsistency here, the terms in the spec are accurate.

    resolve reject 并不相反,并且 onFulfilled 回调与 resolveing 并不完全对应.

    resolve is not the opposite of reject, and the onFulfilled callback does not exactly correspond to resolving.

    或者我缺少什么,对于 resolve()方法, resolve 实际上是比 fulfill 更合适的术语是吗?

    Or is there something I'm missing, and resolve is actually a more appropriate term than fulfill for what the resolve() method does?

    是的.问题是您可以解决(带有一个promise)(或者通常是一个thenable,即具有 then 方法的任何对象),而不是通过该对象来满足已解决的问题.Promise会尝试满足thetheable的结果.否则,当thenable有(将有)错误时,它将拒绝该错误.

    Yes. The problem is that you can resolve with a promise (or in general, a thenable, i.e. any object with a then method), and instead of fulfilling with that object the resolved promise will try to fulfill with the thenable's result. Or when the thenable has (will have) an error, it will reject with that as the reason.

    在MDN中,没有提及可能会拒绝 Promise.resolve()方法.

    实际上有:"返回的承诺将遵循"那个可能发生的事情,并采用其最终状态".如果该最终状态是错误状态,则返回的承诺也将拒绝.

    Actually there is: "the returned promise will "follow" that thenable, adopting its eventual state". If that eventual state is the error state, then the returned promise will reject as well.

    有一个 Promise.reject()方法,但没有 Promise.fulfill()方法.因此,在这里,与 reject 相对应的是 resolve ,而不是 fulfill .

    There is a Promise.reject() method, but no Promise.fulfill() method. So here, the counterpart to reject is resolve, instead of fulfill.

    是的,ES6承诺缺少 Promise.fulfill 方法.刚开始时这有点不一致和令人困惑,但是这样做是有充分理由的:它使您无法获得一个承诺,而这个承诺又要由另一个承诺来实现.您只能解析 ES6承诺,当您传递承诺时,它将接受其结果,而不是承诺对象本身.

    Yes, ES6 promises are lacking a Promise.fulfill method. This is a bit inconsistent and confusing at first, but was done for a good reason: It prevents you from having a promise that is fulfilled with another promise. You can only resolve ES6 promises, and when you pass in a promise it will take its result not the promise object by itself.

    这非常有用,但是也很不方便.有一些库可以做得更好,并且可以满足这种情况,它们就是所谓的"代数承诺"(您可以对此进行更好的推理). Creded 是一个很好的例子,其 Promise.of 方法是与 Promise.reject 真正匹配.

    This is pretty useful, but can be pretty inconvenient as well. There are libraries that do it better and allow this case, they're so-called "algebraic promises" (which you can reason about a lot better). Creed is a good example of that, and its Promise.of method is a real counterpart to Promise.reject.

    这篇关于是否“解析"?始终表示与&“完成& amp; amp; amp; amp; amp; amp; amp;"有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆