I want to use async-await for fs.access and fs.unlink. Both the function returns an error without result callback. so the problem is if the function throws error it directly goes to catch block and continue for next iteration.
const unlink = util.promisify(fs.unlink);
const access = util.promisify(fs.access);
const deleteAssetsCtrl = async (req, res) => {
try {
let iteration = 0;
for (let file of fileUrls) {
const fileUrl = file.fileUrl
const fileLocation = path.resolve(contentFolderPath, fileUrl);
access(fileLocation); // step 1
unlink(fileLocation); // step 2
const deleteRowQuery = `DELETE FROM table WHERE fileUrl = '${fileUrl}'`;
executeQuery(deleteRowQuery); // step 3
if (fileUrls.length == iteration){
res.send("true");
} else {
res.send('false')
}
} catch (error) {
console.log('Error =>', error);
res.send(error);
}
}
Error => Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clientHow to control flow. (steps should be in sequence)
You need to add the await annotation on every async function (basically, everything that returns a promise)
const unlink = util.promisify(fs.unlink);
const access = util.promisify(fs.access);
const deleteAssetsCtrl = async (req, res) => {
try {
let iteration = 0;
for (let file of fileUrls) {
const fileUrl = file.fileUrl
const fileLocation = path.resolve(contentFolderPath, fileUrl);
await access(fileLocation); // step 1
await unlink(fileLocation); // step 2
const deleteRowQuery = `DELETE FROM table WHERE fileUrl = '${fileUrl}'`;
executeQuery(deleteRowQuery); // step 3
if (fileUrls.length == iteration){
res.send("true");
} else {
res.send('false')
}
} catch (error) {
console.log('Error =>', error);
res.send(error);
}
}
That means that everything that you wrap in a promise using util.promisify or any other method, creates an async function that you can await for.
If you need the value returned from the function itself, you'll only have access to the content of the returned value if you await the function since without that keyword, node will just continue with the execution and won't wait for the value to be returned or the async function to finish before continuing on.
You have to put await before each promisified function.
Can you try this :
const unlink = util.promisify(fs.unlink);
const access = util.promisify(fs.access);
const deleteAssetsCtrl = async (req, res) => {
try {
let iteration = 0;
for (let file of fileUrls) {
const fileUrl = file.fileUrl
const fileLocation = path.resolve(contentFolderPath, fileUrl);
await access(fileLocation);
await unlink(fileLocation);
const deleteRowQuery = `DELETE FROM table WHERE fileUrl = '${fileUrl}'`;
executeQuery(deleteRowQuery);
if (fileUrls.length == iteration){
res.send("true");
} else {
res.send('false')
}
} catch (error) {
console.log('Error =>', error);
res.send(error);
}
}
Edit: you'll probably need to use await on the function executeQuery and perhaps promisify it... executeQuery is asynchronous or synchronous?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With