0

I have a piec of code that looks like:

 return user.isCorrectPassword(password, function(_err, same) { 
   assertExposable(same, 'login_fail');
   const token = signToken(user);
   console.log(token, 'TOKENN');
   return token;
 });

The token value in there is correct however I need that token from another piece of code. I realized that I would have to 'await' that code so I can get the token value however I am not sure whats the best way to convert that code to promises in order to do something like

const data = await ...

so data equals to token.

My attempt:

const foo = user.isCorrectPassword(password, function(_err, same) {
  return new Promise((resolve, _reject) => {
    assertExposable(same, 'login_fail');
    const token = signToken(user);
    resolve(token);
  });
});
MMXX11
  • 127
  • 3
  • 12
  • you would have to make it so that `user.isCorrectPassword` actually returns a Promise. So it's the implementation of that function that would need changing. You don't indicate whether that's something you have any control over, or whether it's from some third-party library you can't touch. (But if you're using a library that provides asynchronous functionality and it doesn't use promises in its interface, I would suggest looking for a different library.) – Robin Zigmond Aug 15 '20 at 18:20
  • You'll need to swap the first two lines: `return new Promise((resolve, reject) => { user.isCorrectPassword(password, (err, same) { if (err) reject(err) else … }); })` – Bergi Aug 15 '20 at 18:26

0 Answers0