I am building a small express app that basically just reads the files from a directory renames them and then zips the files , now the reading and renaming part of the files is done synchronously , the ziping part is done asynchronously and then my app renders another view, so my logic is something like below:
/*
// file read and renaming code here
*/
// Zip files code below:
try {
let files_converted = fs.readdirSync(OUTPUT_DIRECTORY);
let file_converstion_async_array = [];
files_converted.forEach( (e , i) => {
file_converstion_async_array.push( () => zip_Directory( `${OUTPUT_DIRECTORY}/${e}` , `${OUTPUT_DIRECTORY}/${e}.zip` ) );
});
console.log('Files are being zipped...');
file_converstion_async_array.reduce( ( promise_chain , current_promise ) => {
return promise_chain.then( p_results => {
return current_promise().then( c_result => {
// console.log('ZIPPED');
});
});
} , Promise.resolve([]) ).then( () => {
console.log('All Files have been zipped...');
});
} catch (err) {
console.error(err);
}
// Finally render a new view
res.render("finish", {
name: req.body.outputPath
});
Now what happens is the syncronious code run and then the new view is rendered and then the asycronious code continues running in the background. What i would like to do is once the asyncronious code is executed for my view to be updated with a new message , how can i do this without showing another new view(which i am not sure is possible because i tried this and i beleive you can use res.render
only once .) , so after all the promises have been executed , i would really like to go ahead and render a new message in the view. How do i go about doing that ?