Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Async await using util promisify?

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 client

How to control flow. (steps should be in sequence)

like image 531
Prashant Tapase Avatar asked Aug 28 '19 08:08

Prashant Tapase


2 Answers

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.

like image 111
Thatkookooguy Avatar answered Oct 26 '22 20:10

Thatkookooguy


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?

like image 31
Stéphane Damon Avatar answered Oct 26 '22 18:10

Stéphane Damon