Catching errors from nested async/await functions
If
etcFunction()
throws, does the error bubble up all the way through theasync function
s?
Yes. The promise returned by the outermost function will be rejected. There's no need to do try { … } catch(e) { throw e; }
, that's just as pointless as it would be in synchronous code.
… bubble up all the way to the top-level Promise?
No. Your topLevel
contains multiple mistakes. If you don't return
the doThing(data)
from the then
callback, it will be ignored (not even awaited) and the rejection stays unhandled. You'll have to use
.then(data => { return doThing(data); })
// or
.then(data => doThing(data))
// or just
.then(doThing) // recommended
And in general, your function should look like this:
function toplevel(onsuccess, onerror) {
makePromise()
.then(doThing)
.then(onsuccess, onerror);
}
No unnecessary function expressions, no .then(…).catch(…)
antipattern (that could lead to onsuccess
and onerror
to both be called).