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

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

问题描述

我对各种不同的术语感到困惑.根据我的理解,承诺可以是:

已完成拒绝待办的定居解决推迟

解决意味着解决吗?或者这是否意味着它实现了?什么是延迟?

解决方案

术语可能很难.
让我们参考 :

<块引用>

一个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:

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天全站免登陆