Prashant Tapase
Prashant Tapase

Reputation: 2147

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)

Upvotes: 9

Views: 7832

Answers (3)

Rahul Sharma
Rahul Sharma

Reputation: 10111

promisify will only convert function to promise. You should use unlink, access with await, await will wait until that promise is resolved.

Note: you may need to use await for executeQuery also because it's async operation depends on it's returning promise or not.

iteration is not incremented anywhere inside the function.

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}'`;
            await executeQuery(deleteRowQuery); // step 3 
        } // is it closed correctly
        if (fileUrls.length == iteration){
            res.send("true");
        } else {
            res.send('false')
        }           
    } catch (error) {
        console.log('Error =>', error);
        res.send(error);
    }
}

Upvotes: 1

Stéphane Damon
Stéphane Damon

Reputation: 241

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?

Upvotes: 2

Thatkookooguy
Thatkookooguy

Reputation: 7012

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.

Upvotes: 5

Related Questions