QZAK
QZAK

Reputation: 63

How to module exports inside an exported function

how do I achieve such thing?

module.exports = {
    run : async function (param1) {
        if (param1 === 1) // Export success: true
        else // Export success: false
    }
};

Basically inside the run function I want to be able to export success to be either true or false depending on how the run goes.

Upvotes: 1

Views: 573

Answers (2)

Shodipo Ayomide
Shodipo Ayomide

Reputation: 1450

You'll only be able to return, not export, exports can only be used in ES modules and can only be added as the last line.

Instead, you’d want to return. When you return the Boolean value, you can read it from the call site. See similar example here

Upvotes: 2

Akash Patel
Akash Patel

Reputation: 190

Instead of exporting value. you should return it from the function.and use that values.

//run.js

module.exports = {
    run: async function (param1) {
        return param1==1 ? true : false
    }
};


//index.js
let {run}=require("run.js")
run(1).then((shouldRun)=>{
    console.log(shouldRun)//true
}).catch((error)=>{
    console.log(error)
})

Upvotes: 1

Related Questions