question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errors thrown in async methods are not forwarded correctly

See original GitHub issue

To 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:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
etamponicommented, Oct 23, 2019

The issues lies at https://github.com/GoogleChromeLabs/comlink/blob/master/src/comlink.ts#L153. I’ve changed it to:

    Promise.resolve(returnValue)
      .catch(err => {
        throwSet.add(err);
        return err;
      })
      .then(returnValue => {

and now it correctly catches errors thrown in async functions.

0reactions
hellboywarcommented, Oct 24, 2019

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found