Búsqueda de sitios web

typescript - Función asíncrona que resuelve una Promesa y luego devuelve un booleano?


Tengo una función async que se supone debe devolver un booleano si existe una entidad basada en una llamada de solicitud anterior. Así es como se ve:

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

Luego, en otra función llamo a vertexExists:

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

Pero parece no esperar a que nbVertices se resuelva y en su lugar me dice inmediatamente false.

Ahora, sé que se supone que las funciones asincrónicas deben devolver un Promise y no un boolean, pero ¿hay alguna manera de tener un comportamiento similar?

¿Me perdí de algo?

Respuesta #1

Estás negando el objeto promise. Necesitarás usar

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

o

if (await this.vertexExists(entity).then(x => !x)) {

Artículos relacionados