I am confused with one behavior of async and await. I have a promise.all
with a map calling another async function. inside that async function I have a file writing fs.writeFile()
feature that has a callback. I want to do something once all the promise.all promises are resolved. But, fs.writefile()
callback is always completing late. i.e. the console written after promise.all
is getting printed first and then zip completion console.
here is the code:
ingite = async () => {
let users = await storage.getData('users');
let result = await Promise.all(
users.map( async user =>{
await datagenerator.fetchAllData(user.user);
})
);
if(result) {
console.log("All Done -->");
}
}
function that was called
fetchAllData = async (userName) => {
await fs.writeFile(`${global.appRoot}/user_data/${userName}/test.html`, template, function(err) {
if (err) {
console.log(err);
} else {
zipFolder(`${global.appRoot}/user_data/${userName}`, `${global.appRoot}/user_zip/${userName}-info.zip`, function(err) {
if(err) {
} else {
const file = `${global.appRoot}/user_zip/info.zip`;
console.log('ZIP generated successfully for -->' , userName);
}
});
}
});
Here, in the code 'ZIP generated successfully for -->'
for some users comes after
console.log("All Done -->");
Can anyone suggest me what is going wrong?