I have a simple asynchronous function that works perfectly. But I need to know what every recursive call returns, so I added then
(see the line (*)
). Unfortunately, result
in such then
’s is undefined
, and the result of the whole function becomes undefined
as well.
async function foo(n) {
try {
if (n == 0) {
return 0;
}
return n + await foo(n - 1)
.then(result => { bar(); }); // (*)
}
catch (e) {
console.log(e);
}
}
Why does this then
break the function? Should I use something else?