2

I'm reading this article about Promise chaining, and it says "a handler may return not exactly a promise, but a so-called “thenable” object". I want to know which of the following is correct:

1) The handler can return a promise or a then-able object, but the then() method containing the handler must return a promise.

2) The handler can return a promise or a then-able object, and the then() method containing the handler can also return either a promise or a then-able object.

gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • 1
    A thenable's `.then()` method need not return either a Promise or another thenable, but the utility of such a thenable would be severely limited. It could only be used at the end of a chain. – Roamer-1888 May 01 '20 at 01:23
  • @Roamer-1888 I understand that a custom thenable's then() method doesn't have to return a promise or another thenable. What I'm wondering is for a Promise (not a custom thenable), is it the then() method or the then() method's handler that has the option to return either a promise or a custom thenable? – gkeenley May 01 '20 at 01:45
  • 1
    If you are asking about Promises, then the answer is simple, [then must return a promise](https://promisesaplus.com/#point-40). Hence, the question is only worth asking if it is about arbitrary thenables, not Promises. – Roamer-1888 May 01 '20 at 02:11
  • Got it, thanks! If you want to post that as an answer I'll accept. – gkeenley May 01 '20 at 02:20
  • 1
    @Bergi's answer says it all. You may accept his answer. The poor boy needs the points :-) – Roamer-1888 May 01 '20 at 14:47
  • 1
    @Roamer-1888 didn't realize you were being sarcastic until I looked at his score. Guy doesn't mess around. – gkeenley May 01 '20 at 20:19

1 Answers1

1

It's not a promise unless its .then(…) method returns a promise, and if it's an ES6 native Promise then it definitely will.

A thenable's then method may return anything (including undefined).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375