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.

Can't pass callback to worker functions

See original GitHub issue

The docs are saying that we can pass functions between worker and main thread, but looks like this is only true on one side. I can pass a callback from Worker to Main Thread but I can not pass it from Main Thread to Worker.

Here is the code I used to test (disregard the timeout. I used it only to prevent race conditions)

const testScript = `
  window.analytics = [];
  window.analytics.track = (title, cb) => {
    console.log('Logging analytics.track arguments', {title, cb});
    cb();
  };
  window.addEventListener('click', () => {
    console.log('Worker > Main Thread callback')
  });
`;
<script
  dangerouslySetInnerHTML={{
    __html: `
      setTimeout(() => {
        window.analytics.track('My event', () => {
          console.log('Main Thread > Worker callback')
        });
      }, 3000);
      partytown = {
        forward: ['analytics.track'],
      };
    `,
  }}
/>
<script type="text/partytown" dangerouslySetInnerHTML={{__html: testScript}}/>

And this are the logs I see in the browser after reloading the page and clicking Screen Shot 2022-04-29 at 6 15 03 PM

As we can see the Main Thread > Worker callback never shows and we can see that the callback is an empty function inside the worker.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
steve8708commented, May 12, 2022

Very cool. If you’re game to take a stab at it and send a PR that passes build and adds an additional test for this, that would be great

0reactions
cameronbraidcommented, Jul 1, 2022

I need something like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to pass a callback to a webworker?
So No. You can't pass functions about. My issue is that I send the worker a message to start the rendering at the...
Read more >
How to pass function to Web Workers - DEV Community ‍ ‍
Passing serialized functions around may be not secure. It's better to define all needed functions inside the worker. ... I generally agree.
Read more >
JavaScript Callbacks Variable Scope Problem - Pluralsight
In this free JavaScript guide, you'll learn how to fix the common JavaScript callbacks variable scope problem. This step-by-step JavaScript ...
Read more >
JavaScript | Passing parameters to a callback function
Callback Function : Passing a function to another function or passing a function inside another function is known as a Callback Function.
Read more >
Hooks FAQ - React
Hooks won't work if you forget to update, for example, React DOM. ... If computing the initial state is expensive, you can pass...
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