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

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

问题描述

我正在查看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""promise" 之间有什么区别?

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

也在第 2.3 节中.承诺解决程序,

Also in Section 2.3. The Promise Resolution Procedure,

promise 解析过程是一个抽象操作,输入一个promise 和一个值,我们表示为[[Resolve]](promise, x).

The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x).

所以我的问题是:

为什么用两个左括号和右括号表示?有什么约定吗?

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

非常感谢.

推荐答案

那么术语thenable"和promise"之间有什么区别?

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

我认为您已经引用的部分确实很好地回答了这个问题:

I think the section you've already cited does answer this very well:

  • thenable 是具有 then 方法的对象.任何对象.
  • promise 是具有 then 方法(即 thenable)的对象符合规范.
  • 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?"

问题在于,通过查看一个对象,您无法确定它是否是一个承诺.
可能能够判断这是一个承诺,因为您可以看到它的 then 方法是由您自己或您信任的人实现的——通常是您选择的承诺库.您将能够看到"这一点,因为该对象确实从您的 promise 原型继承,或者您甚至可以将方法(在引用上)与您定义的函数进行比较.或任何其他适合您的检查方法.
可能能够判断它不是承诺,因为它没有 then 方法.
但是你如何处理一个实现了 then 但不知道是 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() 方法用于 鸭子打字.它确实指定了关于如何处理此类 thenables(可能 是承诺或至少具有类似行为)的精确算法,以便您可以从它们创建实际的、可信的(已知")承诺.

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.

为什么用两个左括号和右括号表示?有什么约定吗?

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

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