Home » Javascript » NodeJs: How to export an async value to another file?

NodeJs: How to export an async value to another file?

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’m trying to access the result from an async function from one file in another file (say index.js).

test.js

const preMovie = async () => {
    const promiseTickets = new Promise((resolve, reject) => {
        console.log("abcde");
        resolve('ticket is here')
    });

    let ticket = await promiseTickets;
    return ticket;
}

preMovie().then((m) => console.log(m));

index.js

//access the value of ticket here

My goal is to export the value inside ticket only when it has completed its task. I don’t want to export an undefined value. I have tried my hand with different solutions, but either they are too complicated for me to understand or didn’t blend with what I was looking for.

Answers: