I am trying to return a json report (containing two parts) as javascript object in my mainFile.js
as shown below:
file1.js
module.exports = async (param1, param2) => {
try {
await fun1(param1, param2);
await fun2();
const resp = await generateReport();
await fun3();
// console log works fine in here.
console.log(resp);
return resp;
} catch (err) {
handleError(err);
}
}
where fun1()
, fun2()
, fun3()
do irrelevant stuff and generateReport()
is shown below:
generateReport = () => {
return new Promise((resolve, reject) => {
console.log("Generating report.");
core.jsonreport((err, resJSON1) => {
if (err) {
throw new Error(`Error: ${err.message}`);
} else {
core.jsonreport2((err, resJSON2) => {
if (err) {
throw new Error(`Error: ${err.message}`);
} else {
resolve(
JSON.stringify({
part1: resJSON1,
part2: resJSON2
}, null, 4));
}
})
}
});
})
}
but when I require and call that function in my main file I get no output.
mainFile.js
const myFunc = require('./file1');
myFunc(param1, param2)
.then(res => console.log(res));
// Console log didn't work here.
Any ideas how I could fix that?
This is the code of the jsonreport function that I need to use.
core.js
file from a package I am using.
/**
* Generates a report in JSON format
**/
Core.prototype.jsonreport = function (callback) {
if (typeof callback === 'function') {
this.api.requestOther('/core/other/jsonreport/', callback);
return;
}
return this.api.requestPromiseOther('/core/other/jsonreport/');
};