Using a try/catch block won't attach an error handler at the right time (before the next event loop tick):
// promise1 will resolve after one second
let promise1 = new Promise(resolve => setTimeout(resolve, 1000))
// promise2 will wait for the next even loop tick to call its error handler
let promise2 = Promise.reject('something bad')
// At this point, you should await promise2, or set an error handler using .catch
// But you can do some sync stuff if you want to:
someExpensiveCPUStuff()
// But if you do anything async, it'll print a warning in Node.js
let result1 = await promise1
If you ever need to store a promise and its error over time (ie. an async cache), you can wrap the value, and unwrap it when needed: let wrapped = promise
.then(result => ({ type: 'resolved', result }))
.catch(error => ({ type: 'rejected', error }))
await doSomeAsyncStuff()
let result = await wrapped.then(data => {
if(data.type === 'resolved') {
return data.result
} else {
throw data.error
}
})