Kevin
Kevin

Reputation: 1367

How do I make this function a promise?

I'm using express to route

app.get('/api/users/:username', (req, res) => {
  let username = req.params.username;
  findUserInApi(username)
    .then(foundUser => {
      if (foundUser) {
        res.status(403).send('User not found in api yet');
      }else{
        res.json(foundUser);
      }
    });
});

I want findUserInApi to be a promise because I need to wait on the fetch that's happening inside that function.

this is findUserInApi

const findUserInApi = (username) => {
  findChattersPerRole()
    .then(chattersPerRole => {
      console.log(`${username} should\'ve been added by now...`);
      //console.log(chattersPerRole);
      let wantedUser = find(chattersPerRole, {username});
      if (!wantedUser) {
        console.log(`${wantedUser} wasn't found m8`);
        return Promise.resolve(null);
      }
      console.log('wanteduser is: ', wantedUser);
      getUserByUsername(wantedUser.username)
        .then(foundUser => {
          console.log('founduser is: ', foundUser);
          return Promise.resolve(foundUser);
        });
    });
};

I used to just return null when not found or foundUser when he was found, but I tried to make promises out of them by returning Promise.resolve. I'm not sure how to get this fixed...

Upvotes: 1

Views: 77

Answers (1)

RyanZim
RyanZim

Reputation: 6984

You need to return your entire promise chain, like this:

const findUserInApi = (username) => {
  return findChattersPerRole()
    .then(chattersPerRole => {
      console.log(`${username} should\'ve been added by now...`);
      //console.log(chattersPerRole);
      let wantedUser = find(chattersPerRole, {username});
      if (!wantedUser) {
        console.log(`${wantedUser} wasn't found m8`);
        return Promise.resolve(null);
      }
      console.log('wanteduser is: ', wantedUser);
      getUserByUsername(wantedUser.username)
        .then(foundUser => {
          console.log('founduser is: ', foundUser);
          return Promise.resolve(foundUser);
        });
    });
};

Note the addition of return in return findChattersPerRole().then().

However, you're nesting promise chains. That's an anti-pattern. Also, as mentioned in the comments, you don't need to use Promise.resolve() in a .then(), it is applied automatically. Your code could be restructured to something like this:

const findUserInApi = (username) => {
  return findChattersPerRole()
    .then(chattersPerRole => {
      console.log(`${username} should\'ve been added by now...`);
      //console.log(chattersPerRole);
      return find(chattersPerRole, {username});
    })
    .then(wantedUser => {
      if (wantedUser) {
        console.log('wanteduser is: ', wantedUser);
        return getUserByUsername(wantedUser.username);
      }
      else console.log(`${wantedUser} wasn't found m8`);
    })
    .then(foundUser => {
      if (foundUser) console.log('founduser is: ', foundUser);
      return foundUser;
    });
};

Upvotes: 2

Related Questions