Errors thrown in async methods are not forwarded correctly
See original GitHub issueTo reproduce:
main.js
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
async function init() {
const worker = new Worker("worker.js");
const obj = Comlink.wrap(worker);
try {
await obj.throwError();
} catch (err) {
console.log("Error caught", err);
}
}
init();
worker.js
importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
const obj = {
// THIS IS THE IMPORTANT BIT
async throwError() {
throw new Error("foo bar");
}
};
Comlink.expose(obj);
If you remove async
from throwError
, the error is caught. Otherwise you get an Uncaught (in promise)
error in the console.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Surprising case where exception handling in async method ...
The main point is method marked with 'async' (doesn't matter if it has await statements or not) will be rewritten by compiler to...
Read more >Why exceptions in async methods are “dangerous” in C#
No exception was thrown because the MyAsyncMethod routine is not awaited and the exception is literally lost with the Task. In that case,...
Read more >Understanding async/await - discord.js Guide
The rejected state means that the Promise encountered an error and could not execute correctly. One important thing to know is that a...
Read more >Futures and error handling - Dart programming language
Solution: Using Future.sync() to wrap your code. A common pattern for ensuring that no synchronous error is accidentally thrown from a function is...
Read more >Mocking exceptions rising inside an async method
As a resume, am I stubbing the call to GetAllAsync() correctly? ... The framework guidelines are to not throw exceptions synchronously from a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issues lies at https://github.com/GoogleChromeLabs/comlink/blob/master/src/comlink.ts#L153. I’ve changed it to:
and now it correctly catches errors thrown in async functions.
I also encountered this bug today while upgrading to latest comlink, but I can’t wait that long, so I fixed it in PR
@surma thank you for the comlink - it’s a delight to work with
P.S. hope you could publish fixed version sooner!