Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  40] [ 6]  / answers: 1 / hits: 15261  / 9 Years ago, fri, january 29, 2016, 12:00:00

I'm just getting started with async/await and running into a problem.



I can do as expected:



async function x() {
let y = await Promise.resolve(42);
return y;
}


But when I reject a Promise:



async function bad() {
try {
await Promise.reject('bad');
} catch(err) {
err; //AssertionError: TypeError: (0 , _errorHandler2.default) is not a function
}
}


How do I catch rejected Promises with async/await?


More From » node.js

 Answers
4

What bad; alone is supposed to do? The error is caught as expected, you just don't do anything with it:



async function bad() {
try {
await Promise.reject('bad');
} catch(err) {
console.log(err);
}
}

bad();


This outputs bad as expected. Code here.


[#63509] Wednesday, January 27, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
willieelisham

Total Points: 201
Total Questions: 108
Total Answers: 106

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;