I am checking out the "Promises/A+" Specification, but could not understand the following things:
On Section 1. Terminology,
1.1. "promise”
is an object or function with a then method whose behavior conforms to this specification.
1.2. “thenable”
is an object or function that defines a then method.
So What is the difference between the terms "thenable"
and "promise"
?
Also in Section 2.3. The Promise Resolution Procedure,
The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x)
.
So my question is:
Why is it denoted within 2 opening and closing brackets? Is there any convention?
Thank you very much.
This is a smart attempt to make it easier for promises to be interoperable between different libraries.
The spec uses the term
thenable
in just a few places. This one is the most important (empasis mine):This will make implementors do a check like:
If the spec instead said "if x is a promise, then...", how would the implementor know whether
x
is a promise or not? There's no practical way to make sure ifx
complies with the Promise spec just by inspecting it.An implementor (say, library
FooPromises
might do something likeand it would effectively reject any promises coming from different implementations.
Instead, by using a super-simple definition of
thenable
in this condition that implementors can easily verify, it's trivial to make this check and you make it possible for implementations to play nice with each other.For you second question, I'm not sure but my idea would be that a notation
[[Resolve]](promise, x)
emphasises that it's an abstract operation. If they dropped the brackets and just saidResolve(promise, x)
, it would somehow imply that implementors should make a real function namedResolve
and expose it.This is not needed -
Resolve
is not part of the promises' interface; it's just a part of their behaviour that was important enough that it was given a name and a separate section in the docs.I think the section you've already cited does answer this very well:
then
method. Any object.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?"
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.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.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.