I'm currently in the process of converting code that used .then/.catch
to instead use async/await
. One particular challenge I'm facing is how to access the original promise that fails within the catch block, for logging purposes.
Here is the original code snippet:
const myPromise = somePromise()
myPromsise.then(() => {
//...
}).catch((error) => {
errorLogger(error, myPromise) // The error logger function requires the original promise to be passed in.
}
And here is how it looks with async/await:
try {
const myPromise = await somePromise()
//...
} catch (error) {
errorLogger(error, myPromise) // Unfortunately, myPromise is not accessible in the catch block due to scope constraints.
}
So the question remains: How can I effectively access the promise within the catch block?