-1
function getFirstUser() {
    return getUsers().then(function(users) {
               return users[0].name;
           });
}

Hi all, I'm learning js on my own and while reading an article on promises I came across this function. I coudn't understand why first return is required when the second return is doing the job. Thnaks in advance

Jeff
  • 6,895
  • 1
  • 15
  • 33
arvin
  • 43
  • 6
  • this is not a well formed question. please consider reading [this](https://stackoverflow.com/help/how-to-ask). For your specific question you need to understand callback functions. take a look at [this SO question](https://stackoverflow.com/questions/37177725/how-to-chain-then-functions-and-callback-success-function-in-angular-js) – ramrunner Dec 30 '17 at 22:26
  • @ramrunner - The question could use a little more explanation, but I understood it just fine and wrote a detailed answer. – jfriend00 Dec 31 '17 at 00:03
  • @jfriend00 i know, i didn't downvote it. just suggested that OP edits it to bring it a bit more up to site standards. – ramrunner Dec 31 '17 at 00:09
  • @ramrunner will keep in mind from next time and thanks a ton guys – arvin Dec 31 '17 at 04:02

2 Answers2

1

First return statement will return instance of the Promise itself.

The second return will fire in callback function, which was passed to the Promise.

Good articles about Promises: MDN, Google developers portal.

Sergii Rudenko
  • 2,603
  • 1
  • 22
  • 24
1

First off, the first return is returning the result of getUsers().then() from your getFirstUser() function. So, the return value from getFirstUser() is a promise.

The second return is returning a value from the .then() promise callback. In the world of promises, returning a value from a .then() handler sets the resolved value of that promise to that value.

The second return is NOT returning from the getFirstUser() function. It's returning only from the .then() callback function and that return value goes back into the promise infrastructure that called the .then() handler.

Timing-wise, it's also useful to understand what happens in a timeline. Here's a step-by-step description:

  1. Someone calls getFirstUser().then(somefn).
  2. As getFirstUser() executes, it calls getUsers(). That function initiates some asynchronous operation (perhaps a database query operation) and then returns a promise (I will call p1). The database operation has not yet completed.
  3. Then, on that returned promise .then(fn) is executed. This just registers the function as a .then() callback and it returns a new promise. The .then() handler is not yet called, it is stored to be called later.
  4. .then() returns a new promise (I will call p2).
  5. Then the function getFirstUser() is done and it returns the p2 promise from the previous step.
  6. Then, sometime later, the getUsers() promise resolves and causes the .then() handler that was previously registered on it to get called.
  7. When that .then() handler gets called, your return users[0].name; executes and returns that value into the promise infrastructure. That value becomes the resolved value of promise p2 which is the promise that was returned from getFirstUser().
  8. Promise p2 is now resolved. This causes the original caller's .then() handler to be called (the one in step #1) and someFn is called and passed the first user resolved value.
jfriend00
  • 683,504
  • 96
  • 985
  • 979