const createPolicyPackage = (filePath, bufferStream) => new Promise(resolve => {
let wstream = fs.createWriteStream(filePath);
wstream.write(bufferStream);
resolve(bufferStream);
});
let policypath = 'pdfappl/' + submissionId + '_' + date + '_policydoc.pdf';
const resultpromise = <some api call giving .pdf file>;
let policybuf = Buffer.from(resultpromise, 'base64');
await createPolicyPackage(policypath, policybuf);
i am using node v10.16.3 with the above code i am able to create a file with data i am receiving from api call in the folder pdfappl, but after that when trying to delete the file using unlink. it is not deleting it. maybe it is getting marked for delete. bcoz the files are only visible till the node process is running.
as soon as i kill the node process, the files dissappears.
fs.unlink(file, function (err) {
if (err) console.log("\n\n\n\n\n\n\nerr ", err)
});
maybe the createWriteStream is not completing the write process. so, i tried writing it like.
const createPolicyPackage = (filePath, bufferStream) => new Promise(resolve => {
let wstream = fs.createWriteStream(filePath);
wstream.write(bufferStream);
wstream.on('close', ()=>{
resolve(bufferStream);
})
});
or
const createPolicyPackage = (filePath, bufferStream) => new Promise(resolve => {
let wstream = fs.createWriteStream(filePath);
wstream.write(bufferStream);
wstream.on('finish', ()=>{
resolve(bufferStream);
})
});
but finish and close events didn't fire, and the code got stuck. how to properly close the writeStream, so that i can delete the file when required