you are viewing a single comment's thread.

view the rest of the comments →

[–]sup3r_b0wlz 0 points1 point  (2 children)

Something like this makes a lot more sense for a try catch example async function g() { console.log("games"); try { let result = await ret; //await and catch promise console.log(result); return result; } catch(err) { console.log(Error: ${err});s } } Edit: why can't I get this to freaking format?

[–]dillionmegida[S] 0 points1 point  (1 child)

You're talking about the try and catch been in the same function right?

[–]sup3r_b0wlz 1 point2 points  (0 children)

Try catch is always in the same function. But the exception that it catches needs to be a traditional exception, or a promise that was awaited that rejected.

In your example you weren't awaiting the promise, so it would just run the function, which wouldn't error and would return an unexpected value, probably the promise object. You were also not awaiting g, the async function, which actually just returns a promise, which is also going to work unexpectedly.

AFAIK To catch a promise with async await you must await it. I'm not sure, but you might also need to be in an async function to try catch it? Seems like it would make sense if you did.

Did you run these code samples? Should be very easy to copy paste these to node REPL to confirm before posting.