Reputation: 11930
To begin with, I am taking a follow up from this question I posted few moments ago
Now, I thought I knew Aysnc and Promise but clearly I am missing something.
Referencing to the Tagged answer by estus,
Unless API supports promises, errors should be entirely handled in async function. Function body should be wrapped with try..catch to rule out unhandled rejections which may result in exceptions in future Node versions
From which I was able to comprehend that whenever we are using aysnc function and we want to do error Handling we need to use try..catch
and for normal Promises we could simple do resolve/reject or if it is already a promise then we can chain and do .then
and .catch
but for that I got following reply on comment
Yes, you can expect an error. async/await is just syntactic sugar for raw promises. Any async function can be rewritten in plain ES6
I might be keeping this question broad but can someone please help me in explaining..
When do we need to use
.then
and .catch
and when do we need to use
try..catch
Also, What does it mean by
Unless API supports promises
Upvotes: 2
Views: 1009
Reputation: 3312
// So this is how promises work (as you may already be familiar with)
function promiseFu() {
return new Promise((resolve, reject) => {
reject();
})
.catch(e => {
// The exception has been handled
console.error("Error begin in promiseFu()!")
throw e; // <- now this triggers the second exception
})
}
// Asynchronous functions are the functions that ALWAYS returns a promise
// therefore, this is also correct
async function asyncFu() {
return new Promise((resolve, reject) => {
reject();
})
.catch(e => {
// The exception has been handled
console.error("Error begin in promiseFu()!")
throw e; // <- now this triggers the second exception
})
.catch(e => {
// Here the second exception gets handled as well, and asyncFu does not throw any exception in the end
})
}
// Now the power of async await
async function asyncMainFu() {
// using await before an async function would make it wait for the function to complete asynchronously before moving on
await asyncFu()
// await would do the same with promises as well
// await promiseFu() // <- this is correct as well
// BUT now, if you see promiseFu() is throwing the second exception which is not yet handled,
// asyncMainFu() would throw the same exception as well. unless handled by a try..catch block
try {
await promiseFu()
} catch(e) {
// handling the exception thrown by promiseFu and not throwing any new exception
// is a good idea if you think caller of asyncMainFu() might not handle it.
}
}
Upvotes: 1
Reputation: 223064
try...catch
in async
function is syntactic sugar for catch()
in raw promise. If raw promises are used for some reason (legacy code) then catch()
may be used. This shouldn't be a problem in Node, since recent versions support async..await
.
Notice that try..catch
catches both synchronous and asynchronous errors in async
. This should be taken into account to not leave synchronous errors unhandled with plain promise.
If API doesn't support promises, you cannot expect that promise rejection that is returned from a function be handled by API, so you need to do this yourself.
Upvotes: 1
Reputation: 2867
Async await code looks cleaner and easy to read. The Promises were created to solve the callback hell problem but chaining a lot of promises also confusing. So async wait is a syntactical sugar and you can use any one of the .then or async await.
If you are using simple promises syntax then you can use .then.then.then.catch() syntax.
if you are using async and await then you have to use try catch. All the await will go in try and the catch condition would go in single catch.
Both these can be used when API/function you are using returns a promise.
Upvotes: 3