11
var p1 = new Promise (function (res, rej){
    res(42);
}).then((result) => {return result;});

**If I have ** return result,

is this promise resolved or not? What does a "resolved promise" mean?

user7361276
  • 229
  • 2
  • 4
  • 11
  • Maybe this question is of help to you, having a seemingly similar misunderstanding: [Why does the Promise constructor require a function that calls 'resolve' when complete, but 'then' does not - it returns a value instead?](http://stackoverflow.com/q/31324110/1048572). Also, regarding the second question, see [What is the correct terminology for javascript promises](http://stackoverflow.com/q/29268569/1048572) – Bergi Jan 03 '17 at 13:59

3 Answers3

9

Java Script is a single threaded language. This simplifies most tasks; but, it means asynchronous tasks must be handled in a callback function. A Promise is an object oriented type of callback that offers greater functionality than a simple callback function.

A resolved promise means that the then function of the promise object will be called. In your example, the promise has been resolved.

A rejected promise means that the catch function of the promise object will be called.

Returning a result in a then function, allows for chaining. Each then result can change or manipulate the result for before passing it on to the next promise in the chain.

In your example, you resolved the first promise and then returned a result for the next promise in the chain which you don't handle so effectively the returned result does nothing.

Scott Boring
  • 2,601
  • 1
  • 25
  • 31
  • Technically speaking, "resolved" is different from "fulfilled", and the `then` handler is called when the promise is fulfilled. "Resolved" could refer to the state of being resolved with another promise, which still has not fulfilled. –  Jan 03 '17 at 02:41
  • So did the "return" resolve the promise? And give it a value? – user7361276 Jan 03 '17 at 07:39
  • No, returning a value did not resolve the original promise object you created. – Scott Boring Jan 03 '17 at 14:28
  • This short reference may be useful for further clarification on the terms: https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md – Andrew May 28 '20 at 14:20
7

Before asking about promises, you should consider what the function

result => result

which is the shorter way of writing your

(result) => { return result; }

actually does. It's called the identity function, which means it returns exactly what was passed in.

As a then handler, then, it means exactly nothing--it's a no-op. So the promise returned by then is in exactly the same state, with exactly the same value (if any), as the promise on which you called then.

Consider closely what promise.then(fn) does. It creates a new promise. As long as promise remains pending, then that new promise also remains pending. If and when promise is fulfilled, it calls fn with the fulfilled value as a parameter, executes fn, and (all else being equal) fulfills the new promise with the returned value from fn. If and when promise is rejected, on the other hand, fn is not called, and assuming no second parameter has been passed to then, then the new promise is placed in a rejected state with the same rejection reason--in essence, in this case, the promise can be considered to be "passed through".

So in the case of

promise.then(identity)

where identity is result => result, when promise fulfills with some value v, then identity is called with a parameter of v, returns the same value v, and fulfills the new promise created by then with that same value--in other words, it creates a new promise which has exactly the same status and value as promise. To put it a different way, it does nothing--or rather, merely creates a new promise identical to the one on which then was called.

In your case:

var p1 = new Promise (function (res, rej){
   res(42); })
.then((result) => {return result;});

is this promise resolved or not? What does a "resolved promise" mean?

The promise created by calling the promise constructor (new Promise) is immediately fulfilled with the value 42, by virtue of calling res(42). (You could also have written Promise.resolve(42), which would mean exactly the same thing.) The then creates a second promise. Since the first promise, on which then is being called, is already fulfilled, the then handler is immediately invoked with the parameter (result) of 42. It returns that same value, so therefore the second promise, the one created by then is then fulfilled with that value of 42. So p1 at that point is a fulfilled promise with a value of 42.

p1.then(v => console.log(v))

You will see 42 logged to the console.

There is nothing particularly magical about any of the above. It all derives from the basic principles of promises: their possible statuses, how to create them if you need to, and the meaning of then. A good tutorial or document such as this one should get you on the right track.

  • The `then` handler won't even be called until the underlying promise is fulfilled. The promise created by `then` is fulfilled by virtue of the underlying promise being fulfilled. The return value from the `then` handler becomes the **value** of the fulfilled promise. –  Jan 03 '17 at 03:11
  • So what fulfills the promise then? I thought "return" inside of a then statement returned a new promise – user7361276 Jan 03 '17 at 04:06
  • No, the `return` does not create a promise, nor does it fulfill a promise. The `then` itself creates a new promise at the moment it is written. The new promise is fulfilled at the moment the promise on which `then` was called is fulfilled. The `return` merely sets the **value** for the new promise which was already created by the presence of `then`, and already fulfilled by the original promise being fulfilled. –  Jan 03 '17 at 04:09
  • "Since the first promise, on which then is being called, is already fulfilled, the then handler is immediately invoked with the parameter (result) of 42. It returns that same value, so therefore the second promise, the one created by then is then fulfilled with that value of 42. So p1 at that point is a fulfilled promise with a value of 42." From this line, you are saying that it returned a value of "42" and fulfiled the promise, yet in the comment above you say "return" does not fulfil a promise? Contradictory? – user7361276 Jan 03 '17 at 04:16
  • the "return" does fulfill a promise indeed. http://stackoverflow.com/questions/41438517/what-does-return-do-in-a-then-statement-promise/41438569#41438569 – user7361276 Jan 03 '17 at 07:38
  • 1
    In defense of @user7361276, there is something about Promises that confuse people (myself included). Your answer here seems really good, I've read it twice already, going back for a third read... maybe then I'll understand... There is lots of good documentation but none of it seems to go in with me... maybe the OP is having the same difficulty. – Morvael Sep 01 '17 at 10:30
0

The promise is resolved with the value result. In the snippet you wrote, the then block doesn't change anything, as it makes the promise to resolve with the result value, just as it would be without the return statement and without the then block.

However, if you would return a different value, then the promise would resolve with that value. And if you would return another promise inside this then, you could "chain" the blocks, that is you could write a next 'then' block which would receive the resolved value of the returned promise in the former then block.

A resolved promise means, that the code handled by the promise is done, and the code in the callback passed to the then method is executed with the resolved value passed in.

alek kowalczyk
  • 4,896
  • 1
  • 26
  • 55