0

I have a promise that returns a promise.

For the moment my code is :

async isRepo() {
  return await this.gitPromise.checkIsRepo();
}

enter image description here

But I need to return the value of the promise (true or false) to use the function isRepo() at other places.

Even with "then", I didn't success to return the value of the promise and not the promise itself.

I will need it to do for instance :

if (await isRepo()) {
  // true : todo
} else {
  // false : something else
}
Nulji
  • 447
  • 3
  • 9
  • 4
    No, you can't return the value - it doesn't exist yet when you are calling the function. You can only return the promise - which you already do - and then `await` it in your `if` condition - which you already do and which should work. – Bergi Jan 27 '19 at 14:52
  • The await will not work in the if statement unless that if statement is within an async function. – Randy Casburn Jan 27 '19 at 14:54
  • Btw, [drop the pointless `return await`](https://stackoverflow.com/a/43985067/1048572). Just `isRepo() { return this.gitPromise.checkIsRepo(); }` – Bergi Jan 27 '19 at 14:54
  • 1
    Why did you [add the `await` in an edit](https://stackoverflow.com/posts/54389363/revisions)? Do you have a problem at all? What's the error, what is not working? – Bergi Jan 27 '19 at 14:55

1 Answers1

1

You can do something like this :

isRepo() {
    return gitPromise().checkIsRepo();
  }

And in an other function :

async myFunction() {
   if (await isRepo()) {
     // do my stuffs
   else {
     // something else
   } 
}
Clément Drouin
  • 385
  • 1
  • 4
  • 12