javascript q.js:resolve() 和fulfill() 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18280375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:19:56  来源:igfitidea点击:

q.js: difference between resolve() and fulfill()

javascriptasynchronouspromiseq

提问by alecf

I'm still unclear on the difference between calling a resolver's resolve() vs fulfill()? I see both the functions and the terms "resolve a promise" and "fulfill a promise" batted around a lot.

我仍然不清楚调用解析器的 resolve() 与满足 () 之间的区别?我看到函数和术语“解决承诺”和“履行承诺”都受到了很多打击。

When should I be using each?

我应该什么时候使用它们?

回答by Kris Kowal

You should use resolve. deferredPromise.resolve(nextPromise)means that anything waiting for deferredPromisewill now wait for nextPromise. If nextPromiseis not a promise at all, it gets turned into a fulfilled promise which goes on to inform anything waiting for it that the value has become available.

你应该使用resolve. deferredPromise.resolve(nextPromise)意味着任何等待的东西deferredPromise现在都会等待nextPromise。如果nextPromise根本不是承诺,它就会变成一个已履行的承诺,它会继续通知等待它的任何东西该值已可用。

The fulfillmethod is a bad idea that will be deprecated and eventually go away entirely. fulfillis semantically equivalent to resolvein all useful cases. It's only reason to exist is that deferredPromise.fulfill(value)is easier for humans to interpret than deferredPromise.resolve(value), since resolveis overloaded to handle both nextPromiseand finalValue.

fulfill方法是一个坏主意,将被弃用并最终完全消失。fulfill在语义上等同resolve于所有有用的情况。存在的唯一原因是它deferredPromise.fulfill(value)比 更容易被人类解释deferredPromise.resolve(value),因为resolve处理nextPromisefinalValue.

The problem with fulfillexisting at all is that deferredPromise.fulfill(rejectedPromise)is semantically paradoxical.

存在的问题fulfilldeferredPromise.fulfill(rejectedPromise)语义上是矛盾的。

回答by alecf

In general, resolvemeans to EITHER succeed or fail. That is what triggers the invocation of the thenactions. It can happen exactly once for any given promise.

一般来说,解决意味着要么成功要么失败。这就是触发then动作调用的原因。对于任何给定的承诺,它可以只发生一次。

fulfillmeans to "resolve" successfully. That will trigger the success callbacks in the thenactions. The counterpart of "fulfill" for failure is reject.

完成意味着“解决”成功。这将触发操作中的成功回调then。失败的“满足”对应物是拒绝

From a different perspective, you can categorize the status of any promise at a particular point in time as "unresolved" (sometimes also called pending) or "resolved", and "resolved" has the sub-statuses of "fulfilled" and "rejected". A promise in "fulfilled" status has a value, and a promise in "rejected" status has a reason.

从不同的角度来看,您可以将任何承诺在特定时间点的状态分类为“未解决”(有时也称为待处理)或“已解决”,“已解决”具有“已完成”和“已拒绝”的子状态”。处于“已履行”状态的承诺具有价值,处于“已拒绝”状态的承诺具有原因

The particular methods in each API used to represent these notions differ. And unfortunately, there are many blog posts and documents out there which confuse these terms, in particular using "fullfill" when they mean "resolve" or vice versa.

每个 API 中用于表示这些概念的特定方法各不相同。不幸的是,有许多博客文章和文档混淆了这些术语,特别是当它们的意思是“解决”时使用“fullfill”,反之亦然。

Q

I do not know Q very well, but it appears that its resolvemethod actually fulfillsthe promise (emphasis added):

我不是很了解 Q,但看起来它的resolve方法实际上实现了承诺(强调):

Calling resolve with a non-promise value causes promise to be fulfilledwith that value.

使用非承诺值调用 resolve 会导致使用该值实现承诺 。

The twist, however, is that you can also call deferred.resolvewith a promise, in which case the first promise more or less assumes the state of the passed-in promise. For instance, if the passed-in promise is in "pending" state, the promise adopts the pending state of the passed promise. That implies the slightly odd semantics that a method named resolveactually does not resolve the promise.

然而,转折点是你也可以deferred.resolve用一个 promise调用,在这种情况下,第一个 promise 或多或少假定传入 promise 的状态。例如,如果传入的promise 处于“pending”状态,则promise 采用传入promise 的pending 状态。这暗示了一个稍微奇怪的语义,即命名的方法resolve实际上并没有解决承诺。