I solved it by wraping the fetchReport function with new Promise, after doing some logging I realized that the fetchReprot (which has the write stream) is finishing before sending the file to client:
var fetchReport = (path, data, functionName) => {
return new Promise(async resolve => {
let jsr = process.env.JSREPORTURL + "/api/" + functionName;
let options = {
// auth: { user: 'admin', password: 'samer'},
url: jsr, //valid string to report api
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
responseType: "stream",
maxContentLength: Infinity,
maxBodyLength: Infinity,
};
let response = await axios.post(
jsr,
{
template: { name: "invoice" },
data: data,
},
options
);
let writeStream = response.data.pipe(fs.createWriteStream(path))
.on('finish', () => {
console.log("ONE");
resolve(writeStream);
})
})
};
But I am not sure why it works, I thought that a Async function should behave like a function returning a new Promise?