1//create a Promise
2var p1 = new Promise(function(resolve, reject) {
3 resolve("Success");
4});
5
6//Execute the body of the promise which call resolve
7//So it execute then, inside then there's a throw
8//that get capture by catch
9p1.then(function(value) {
10 console.log(value); // "Success!"
11 throw "oh, no!";
12}).catch(function(e) {
13 console.log(e); // "oh, no!"
14});
15
1// errors that occur in asynchronous code inside promises cannot be caught with regular try/catch
2// you need to pass the error handler into `.catch()`
3
4fetchUserData(userId).then(console.log).catch(handleError);