-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aync forEach handler swallows exceptions #11163
Comments
@Roam-Cooper This isn't how asynchronous code works in JavaScript or how async function outer () {
await Promise.all([1].map(async (i) => throw 'oh noes'))
} |
I know how asynchronous code works in Javascript... And using Promise.all was going to be my solution if a resounding "NO" was the answer to my request, as it frequently is on this repo. Your example doesn't address the problem of not being able to interact with the promise used in an asynchronous forEach handler at all. If this is considered a non-issue then I know I can just use Promise.all, if so then it seems the point of allowing forEach to be asynchronous is only to allow asynchronous calls within the handler but with the critical drawback of losing the ability to handle any sort of rejection within the asynchronous forEach handler. |
I second Blake. I think it's by design. Think of this:
In this case, you also have no way to interact with the return, because There's another function for that,
This will give you all the promises returned. The |
I can see what you mean, but I still think that "getting a returned value" is very different to "catching a thrown exception" inside the forEach loop, because an exception inside a synchronous forEach loop would be correctly thrown which means the behaviour is the polar opposite from synchronous to asynchronous. Since it seems like people disagree I'll close this issue for now, with the idea that it can be reopened or referenced in the future if the "typescript guys" decide that the behaviour really is odd and I'm not just crazy. 😰 Bubbles an exception: [1].forEach(i=>{throw i}) Does not bubble an exception: [1].forEach(async (i)=>{throw i}) |
@Roam-Cooper That's just how asynchronous code works. This isn't actually specific to TypeScript, it's plain JavaScript. You can try checking out the [].forEach(async (x) => { // (1)
await wait(500) // (2)
throw new Error('boom') // (4)
})
// (3) |
Have you ever used ES6 generators? Perhaps understanding the runtime behaviour there could help too (http://www.2ality.com/2015/03/es6-generators.html). Feel free to skip if you've never used. Another way to think about it - |
TypeScript Version: 1.8.10
Code
Expected behavior:
Exception is propagated outside of the forEach.
Actual behavior:
Exception is swallowed by the forEach and there is no way to capture or handle it (as far as I know).
In my use case code similar to the above is wrapped in an async function and I expected the exception to be propagated to the code I have that handles such things.
The text was updated successfully, but these errors were encountered: