问题描述
我正在检查Promises / A +规范,但无法理解以下内容:
I am checking out the "Promises/A+" Specification, but could not understand the following things:
关于第1节。术语,
1.1。 promise
是一个带有then方法的对象或函数,其行为符合此规范。
1.1. "promise"
is an object or function with a then method whose behavior conforms to this specification.
1.2。thenable
是定义then方法的对象或函数。
1.2. "thenable"
is an object or function that defines a then method.
那么术语之间有什么区别thenable
和承诺
?
So What is the difference between the terms "thenable"
and "promise"
?
同样在第2.3节。承诺解决程序,
Also in Section 2.3. The Promise Resolution Procedure,
承诺解决程序是一个抽象操作,将promise和值作为输入,我们将其表示为 [[Resolve]](承诺,x)
。
The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x)
.
所以我的问题是:
为什么在2个开始和结束括号内表示?有没有约定?
Why is it denoted within 2 opening and closing brackets? Is there any convention?
非常感谢。
推荐答案
我认为你已经引用的部分确实回答了这个问题ell:
I think the section you've already cited does answer this very well:
- 一个对象是一个带有
然后
方法的对象。任何对象。 - 一个承诺是一个带有
然后
方法的对象(即一个可用的)符合规范的 。
- A thenable is an object with a
then
method. Any object. - A promise is an object with a
then
method (i.e. a thenable) that conforms to the specification.
到目前为止这么简单。我认为你的实际问题是:为什么要区分?
So far so simple. I think your actual question is: "Why are they distinguished?"
问题在于,通过查看对象,你无法决定是否是一个承诺。
你可能能够告诉它是一个承诺,因为你可以看到它的然后
方法由您自己或您信任的人实施 - 通常是您选择的承诺库。您将能够看到因为对象确实从您的promise原型继承,或者您甚至可以比较与您定义的函数相同的(引用)方法。或者任何其他适合您的检查方法。
您可能能够告诉它不是承诺,因为它没有然后
方法。
但你怎么处理一个实现然后
的对象,但不知道是一个承诺?它是 thenable ,并将按此处理。
The problem is that by looking at an object you cannot decide whether it is a promise.
You might be able to tell that it is a promise because you can see that its then
method is implemented by yourself or someone you trust - the promise library of your choice usually. You would be able to "see" that because the object does inherit from your promise prototype, or you can even compare the method being (referentially) identical to the function you've defined. Or any other inspection method that is sufficient for you.
You might be able to tell that it is not a promise because it has no then
method.
But what do you do with an object that implements then
, but is not known to be a promise? It's a thenable, and will be handled as such.
Promises / A +规范旨在实现promise实现之间的互操作性,并使用它的存在:
Yes, the ECMAScript specifications use this syntax for internal methods and properties:
这些属性实际上并不需要存在,它们纯粹用于描述应该发生的事情 - 实现必须像一样使用它们。它们完全是抽象的操作。
Those properties do not actually need to exist, they're purely used to describe what should happen - an implementation must act as if it used them. They are totally abstract operations though.
这篇关于关于Promises / A +规范,术语“可以”和“可用”之间的区别是什么。和“承诺”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!