关于Promises / A +规范,术语“可以”和“可用”之间的区别是什么。和“承诺”? [英] Regarding Promises/A+ Specification, what is the difference between the terms "thenable" and "promise"?

查看:171
本文介绍了关于Promises / A +规范,术语“可以”和“可用”之间的区别是什么。和“承诺”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查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?

非常感谢。

推荐答案


那么术语可以和承诺有什么区别?

So What is the difference between the terms "thenable" and "promise"?

我认为你已经引用的部分确实回答了这个问题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实现之间的互操作性,并使用它的存在鸭子打字的 .then()方法。它确实指定了一个关于如何处理这些可能性的精确算法(可能是承诺或至少具有相似的行为),以便您可以从它们创建实际的,可信的(已知)承诺。 / p>

The Promises/A+ specification aims for interoperability between promise implementations, and uses the existence of a .then() method for duck typing. It does specify an exact algorithm on how to treat such thenables (that might be promises or at least have similar behaviour) so that you can create an actual, trusted ("known") promise from them.


为什么在2个开始和结束括号内表示?有没有约定?

Why is it denoted within 2 opening and closing brackets? Is there any convention?

是的,ECMAScript规范使用这个语法内部方法和属性

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 +规范,术语“可以”和“可用”之间的区别是什么。和“承诺”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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