I recently found myself in a situation very difficult to debug.
Imagine the following very simple situation:
function somePromise() {
return new Promise((resolve, reject) => {
SomeModule.someMethod();
AnotherModule.somePromise().then(resolve).catch(reject);
});
}
SomeModule
Is there a way to automatically try/catch every Promise in my code with an error handler?
This is implicitly done by the Promise
constructor already. If the callback throws an exception synchronously, the promise will reject.
However, you really should avoid the Promise
constructor antipattern! Use
function somePromise() {
SomeModule.someMethod();
return AnotherModule.somePromise();
}