Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async function which resolve a Promise and then returns a boolean?

I have an async function that supposed to return a boolean if an entity exist based on a previous request call. Here is how it looks like:

async vertexExists(properties) {
    const nbVertices = await this.countVertices(properties);
    if (nbVertices !== 0) {
      return true;
    }
    return false;
  }

Then in another function I call vertexExists:

if (await !this.vertexExists(entity)) {
        const response = await this.gremlinQuery(query);
        return response.body.result.data;
      }

But it seems not to wait nbVertices to be resolved and instead tells me immediately false.

Now, I know asynchronous functions are supposed to return a Promise and not a boolean but is there anyway to have a similar behaviour?

Did I miss anything?

like image 359
Baptiste Arnaud Avatar asked Oct 22 '25 05:10

Baptiste Arnaud


1 Answers

You are negating the promise object. You will need to use

if (!(await this.vertexExists(entity))) {

or

if (await this.vertexExists(entity).then(x => !x)) {
like image 101
Bergi Avatar answered Oct 23 '25 20:10

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!