问题描述
我对各种不同的术语感到困惑.根据我的理解,承诺可以是:
已完成拒绝待办的定居解决推迟
解决意味着解决吗?或者这是否意味着它实现了?什么是延迟?
术语可能很难.
让我们参考 :
一个promise被解决如果它被解决或者它被锁定"以匹配另一个promise的状态.尝试解决或拒绝已解决的承诺没有任何效果.一个承诺是未解决的如果它没有被解决.未解决的承诺始终处于挂起状态.已解决的承诺可能处于待处理、履行或拒绝状态.
什么是延迟?
延迟结果意味着您返回结果的(异步)承诺,而不是直接(同步)返回结果.并且还返回延迟拒绝而不是同步抛出.
请注意,某些库中也使用了defer"(Q) 作为构造 Deferred
对象的方法名称 - 请参阅 此答案Deferred、Promise 和 Future 之间的区别 一个很好的解释.
哦,永远不要相信变量名:defer
也可能是一个缩写的deferredObject".
I'm getting confused with the different terminology thrown around. From my understanding, a promise can be:
fulfilled
rejected
pending
settled
resolved
defer
Does resolve mean settled? or does it mean its fulfilled? and what the heck is defer?
Terminology can be hard.
Let's take from the Promises/A+ specification and the respective ES6 section that there are 3 states:
- pending - the promise does not have taken a value yet, it's future is still uncertain.
- fulfilled - the promise successfully got a result value "assigned"
- rejected - the promise is given a reason why no result could be acquired, typically an error.
The term settled is a hyperonym for fulfilled and rejected, meaning either - the opposite of pending.
The dynamic verbs fulfill and reject describe changing the state from pending into either the fulfilled or rejected. These transitions are called fulfillment or rejection of the promise.
Those were easy. Now, resolve is a different beast. It sometimes is used synonymous to "fulfill", but it would better be understood as settling the promise's fate to either fulfilled or rejected. The resolution (seldom: settlement) of a promise means that it leaves the pending state. But not even that is accurate - the problem is the recursive nature of the Promise Resolution Procedure:
- resolving a promise with a "plain" value means fulfilling it
resolving a promise with a promise (or thenable) means adopting its state:
- resolving with a fulfilled promise is a fulfillment
- resolving with a rejected promise is a rejection
- resolving with a pending promise means waiting for its resolution
Yes, if a promise is resolved it might not even be known whether it's going to be fulfilled or rejected. But it means the fate is no longer undetermined, as it's bound to the promise that we resolved with (notice that you can resolve a promise only once).
Ignoring this special case, a resolved promise usually means a settled promise.
Or, to cite the ECMAScript 6 Specification:
Deferring a result means that you return an (asynchronous) promise for the result, and not the result directly (synchronously). And also return a deferred rejection instead of throwing synchronously.
Notice that "defer" is also used in some libraries (Q) as the method name to construct a Deferred
object - see this answer on The differences between Deferred, Promise and Future for a good explanation.
Oh, and never trust variable names: defer
might as well be an abbreviated "deferredObject".
这篇关于javascript 承诺的正确术语是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!