javascriptnode.jsexpressasynchronous

Calling a async function


I have a module as follows:

async.js

module.exports = async function (){
    await func()
}

index.js

var asyncFun = require('async') 

How do i run it directly as asyncFun() in index.js ?

I know how to run these functions as middlewares but i want to run it directly. As for running it i need to call await and await cannot be called without being inside async.


Solution

  • I solved this question by making the root function async and wrapping all the async function with a normal function and the async function returning a promise. ( using a calling).

    Example :

    Assume this is the main function where you write all the code.

    // create it as a async function
    module.exports = async function main(){
        // Do something all your stuff
    }
    

    Call your main function from else where

    require('./main)()