December 2017
Beginner
372 pages
10h 32m
English
As async-await are just promises, to handle any exception we can follow the same process we did in case of promises; that is, use the promise API's catch method to handle the failure scenarios. The following is the modified code for line number 13, where now along with handling the success scenario we are also handling the failure case:
let p = callAsyncFunction(1).then(x=>console.log(x)) .catch(errorMsg => console.log(errorMsg));
You can see we have chained the catch method to our then function. So, if the function callAsyncFunction returns an error case, the catch method will be called and we can handle the exception as required.
Read now
Unlock full access