James
James

Reputation: 207

NODEJS: Call async function outside of class

Basically, what I have is a file which contains a class MyClass which then contains some other functions within it which is being called by a separate file which works perfectly fine. However, I want to add an async function outside this class in the same file, and then call/execute it from one of the functions inside the class. It would look something like this:

async function myAsync(){

  //do stuff here

}

// Main class
class MyClass {

    firstFunction() {
       //call async function myAsync here
    }
}

// Exports class back to other file which runs it
module.exports = MyClass;

Upvotes: 1

Views: 1385

Answers (1)

аlex
аlex

Reputation: 5698

// file1
async function myAsync(){


}

 class MyClass {

    async firstFunction() {
        return await myAsync();
    }
}

module.exports = MyClass;



// file2

const my = new MyClass();

(async()=> {

  await my.firstFunction();
})()

Upvotes: 3

Related Questions