0

I want to get a boolean value from a promise.

Method One

get tokenValid(): Promise<Boolean> {
return new Promise((resolve, reject) => {
  this.storage.get('expiresAt')
    .then((expiresAt) => {
      resolve(Date.now() < expiresAt);
    })
    .catch((err) => {
      reject(false);
    });
  });
}

Method Two

get tokenValid(): Promise<Boolean> {
return new Promise((resolve, reject) => {
  this.storage.get('expiresAt')
    .then((expiresAt) => {
      resolve(Date.now() < expiresAt);
      return Promise.resolve(true); // difference is here
    })
    .catch((err) => {
      reject(false);
      return Promise.reject(false); // difference is here
    });
  });
}

One and two are similar. The difference is in method Two there is the return statement either true or false. To get the value, just call the method as

if(this.tokenValid()) {
    console.log('return true');
} else {
    console.log('return false');
}

UPDATE:

By the comments, I modified the methods as

Method Three

get tokenValid(): Promise<Boolean>() => new Promise((resolve, reject){
     this.storage.get('expiresAt')
    .then((expiresAt) => {
      resolve(Date.now() < expiresAt);
    })
    .catch((err) => {
      reject(false);
    });
 }

To get the value, just call the method as

if(this.tokenValid()) {
    console.log('return true');
} else {
    console.log('return false');
}

I understand I have to use .then(). But why my method always return true?

Hello
  • 796
  • 8
  • 30
  • 1
    Why do you need to return? You have the `resolve` and `reject` methods for the returned promise. – Taplar Oct 09 '20 at 19:22
  • 2
    If you want a promise to be resolved to `false` in case of error you need to use `resolve(false)` in catch block. Also you can't get the value immediately `if(this.tokenValid())` makes no sense. – Yury Tarabanko Oct 09 '20 at 19:25
  • That's a case where you should *not* use `new Promise`: you already get a promise. Wrapping a promise in a `new Promise` with `resolve` and `reject` calls is adding nothing more to what you already have. – trincot Oct 09 '20 at 19:25
  • 1
    You can't get the underlying value of the `Promise` in an `if` statement like `this.tokenValid()`. `if(this.tokenValid()) {` will always be truthy. You need to `.then()` or `await` tokenValid to get the underlying value. That's the real issue in the code, not how `true` or `false` is returned. – Alexander Staroselsky Oct 09 '20 at 19:25
  • this.tokenValid() will return a Promise, not a boolean. When that promise resolves you can get the boolean value. Your method one seems the usual method to work with Promises. – BigJ Oct 09 '20 at 19:28
  • 1
    @BigJ, I disagree with that last phrase. It is an antipattern. – trincot Oct 09 '20 at 19:28
  • ↑ No need to make a new promise when you already have a promise. – Taplar Oct 09 '20 at 19:29
  • Hi, thanks for the comments. If I want to return a false value when exception/error, can I use `reject(false)` or I have to use `resolve(false)`? – Hello Oct 09 '20 at 19:35
  • 1
    It depends entirely if that is a success case for the promise, or if it is an error. `resolve` is for successes, regardless of the data. `reject` is for errors. So if the "operation is a success" and as part of the success it can return true or false, use resolve. If the value being false means there was an error with the operation, reject it – Taplar Oct 09 '20 at 19:39
  • Updated the question, if it is still duplicated, please let me know so I would delete the question. – Hello Oct 10 '20 at 11:39

0 Answers0