admin管理员组

文章数量:1314025

async function(req, res) {
    try {
        const user = await userCtrl.getUser();
        const userMaps = await mapsCtrl.findDetails(user.mapId);
        res.send(userMaps);
    } catch (error) {
        //handle error
        res.status(400).send(error)
    }

}

// user controll

function getUser() {

    return new Promise(function(resolve, reject) {
        //data base read using mysql
        req.app.get("mysqlConn").query(query, function(error, results, fields) {
            if (error) {
                reject(error);
            }
            resolve(results);
        });

    })
}

//maps controller function is also like above one.

This is the code handle part of an express get route. Sometimes the rejected code is not getting caught. I get the error returned from MySQL in 200 status code.

async function(req, res) {
    try {
        const user = await userCtrl.getUser();
        const userMaps = await mapsCtrl.findDetails(user.mapId);
        res.send(userMaps);
    } catch (error) {
        //handle error
        res.status(400).send(error)
    }

}

// user controll

function getUser() {

    return new Promise(function(resolve, reject) {
        //data base read using mysql
        req.app.get("mysqlConn").query(query, function(error, results, fields) {
            if (error) {
                reject(error);
            }
            resolve(results);
        });

    })
}

//maps controller function is also like above one.

This is the code handle part of an express get route. Sometimes the rejected code is not getting caught. I get the error returned from MySQL in 200 status code.

Share Improve this question edited Oct 10, 2017 at 11:11 CodingIntrigue 78.6k32 gold badges175 silver badges177 bronze badges asked Oct 10, 2017 at 10:11 Shameer S NShameer S N 1031 silver badge8 bronze badges 7
  • 2 Is this ok? Is you current code not working? If it is and you simply asking for an opinion on your implementation than I believe this question could probably be closed as primarily opinion-based Otherwise, if you have an issue, please post your actual code with the details of the issue. – Nope Commented Oct 10, 2017 at 10:14
  • @Fran Yes. I have some issue. Some time rejected code not going to catch block. Await functions are MySQL database read. Mysql errors are catching but always not. – Shameer S N Commented Oct 10, 2017 at 10:34
  • 1 In that case please post the minimum required relevant actual code to create a Minimal, Complete, and Verifiable example preferably in a working snippet to demonstrate the issue. – Nope Commented Oct 10, 2017 at 10:36
  • @Fran I will update asap – Shameer S N Commented Oct 10, 2017 at 10:37
  • How do you know there's an error when it's not caught? – Bergi Commented Oct 10, 2017 at 12:45
 |  Show 2 more ments

1 Answer 1

Reset to default 6

Yes you can write multiple awaits in a single try catch block. so your catch block will receive the error if any of the above await fails. Refer this link for more information about async-await - https://javascript.info/async-await

reject() or resolve () doesn't mean the functions is terminated. So we have to explicitly return from the function to avoid further code execution. In your case put resolve in else block or just put return statement in the if block after the reject is called! Refer this link for more information:- Do I need to return after early resolve/reject?

I hope this help :) Regards.

本文标签: nodejsCan I add multiple await call inside a single try block in JavascriptStack Overflow