Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Nesting async class methods / functions in JavaScript

so this is my code for the class and what i am trying to do with it

const getExternalDbInfo = require('./helperJS/get-external-db-info')
class ExternalDb {
    constructor(extdb_user, extdb_password, extdb_privilege, extdb_ip, extdb_port, extdb_sid) {
        this.extdb_user = extdb_user,
        this.extdb_password = extdb_password,
        this.extdb_privilege = extdb_privilege,
        this.extdb_ip = extdb_ip,
        this.extdb_port = extdb_port,
        this.extdb_sid = extdb_sid
    }
    async getDbData(){
        let data=await getExternalDbInfo(this.extdb_user, this.extdb_password, this.extdb_privilege, this.extdb_ip, 
            this.extdb_port, this.extdb_sid);
        return this;
    }
    async getTablespace(){
        console.log(  getDbData().tablespace_data)
        return  getDbData().tablespace_data

    }
}

const newDb= new ExternalDb("***");

(async function () {
    let x =await newDb.getDbData().getTablespace()
console.log(x)
})();

so i am trying to chain to the class methods (getDbData().getTablespace()) so that i don't have to call the external db more than once. but i get following error: (node:14468) UnhandledPromiseRejectionWarning: TypeError: newDb.getDbData(...).getTablespace is not a function. Is this even possible or is it something i am not understanding because i am a noob in async programming. i have tried returning {this, data} for the getDbData() method, but it didn't work

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Not sure if "nesting" is the right term here, it sounds like you are trying to call the one method from the other. The correct way to do that is

class ExternalDb {
    …
    async getDbData(){
        const data = await getExternalDbInfo(this.extdb_user, this.extdb_password, this.extdb_privilege, this.extdb_ip, this.extdb_port, this.extdb_sid);
        return data;
//             ^^^^
    }
    async getTablespace(){
        const data = await this.getDbData();
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        console.log(data);
        return data.tablespace_data;
    }
}

You would then not chain anything, but simply call

const x = await newDb.getTablespace();
console.log(x);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda