[node.js] How does async/await handle exceptions gracefully?

The world of node.js starts with callbacks and doesn't end with async.

Everyone is scolding why they can't fully evolve. In fact, I feel that this is the carefulness of foreigners, in order to inherit. This is why async is actually the same as promise. If it is not the same thing, how can it be connected. Node.js exception handling has always been criticized, in fact, why can't you be elegant? This is the spicy chicken by design....well, I'm just a spicy chicken with spicy chicken.

Some things can't be changed, you can only change yourself. I googled and I probably found 2 ways that I like.

1. I just console it, I don't deal with it.


async function getData(){
  const a = await someFunction().catch((error)=>console.log(error));
  const b = await someOtherFunction().catch((error)=>console.log(error));
  if(a && b ) console.log("some result")
}

The rules of convention


const go = async () => {
    const readFileResult = await sureThing(readFile('some.json'));
    if (readFileResult.ok) {
        const {
            ok,
            error,
            data
        } = await sureThing(parseJSON(data));
        if (ok) {
            // use our data
        } else {
            return {
                ok,
                error
            };
        }
    } else {
        return readFileResult;
    }
};

The above two methods, I think are more suitable for now. The first kind of not dealing with it seems to be more Buddha-nature. The second, while retaining the synchronous writing method, can also handle asynchronous, complementing each other.

https://medium.com/tech-buddy/async-await-without-try-catch-in-javascript-fdd38abf7e90
https://dzone.com/articles/easier-error-handling-using-asyncawait

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807181&siteId=291194637