Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export the value returned from an async/await function in ES6

Is it possible to export the value of an await function from an ES6 class? For example, I have a class with a function named getDetails which looks like this

class Config {
    static async getDetails() {
        let response = await fetch('url/where/details/are')
        let data = await response.json()
        return data;
    }
}

export { Config }

When I import this class into another file, and call User.getDetails(), whats returned is Promise {<pending>}. Is there a way to make the stack wait for a response from the API first?

like image 685
dave Avatar asked Oct 25 '25 04:10

dave


1 Answers

Why not just use await in your parent file too?

// your async function

const res = await User.getDetails()

// here you have res

Alternatively, you can use .then

User.getDetails()
.then(res => {
    // use res here
})
like image 127
mehulmpt Avatar answered Oct 27 '25 17:10

mehulmpt