什么是javascript承诺的正确术语 [英] What is the correct terminology for javascript promises

查看:117
本文介绍了什么是javascript承诺的正确术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对所抛出的不同术语感到困惑。根据我的理解,承诺可以是:

 履行
拒绝
待定
结算
已解决
延迟

解决方案是否已解决?还是意味着它实现了?什么是推迟?

解决方案

术语可能很难。

让我们从:


承诺已解决如果已解决,或者已被锁定以匹配另一个承诺的状态。尝试解决或拒绝已解决的承诺无效。如果未解决,则承诺未解析。未解决的承诺始终处于暂挂状态。已解决的承诺可能正在等待,履行或拒绝。








什么是推迟?


推迟结果意味着你返回结果的(异步)承诺,而不是直接(同步)的结果。并且还返回延迟拒绝而不是同步投掷



请注意,某些库中也使用了延迟 Q )作为构建 Deferred 对象的方法名称 - 请参阅关于延迟,承诺和未来 之间的差异的答案,以获得一个很好的解释。

哦,永远不要信任变量名称: 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:

A promise is resolved if it is settled or if it has been "locked in" to match the state of another promise. Attempting to resolve or reject a resolved promise has no effect. A promise is unresolved if it is not resolved. An unresolved promise is always in the pending state. A resolved promise may be pending, fulfilled or rejected.


and what the heck is defer?

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承诺的正确术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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