Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!