0

I know that there are several questions, targeting this topic. But I haven't found a solution for my problem.

There is a webworker in my project which should manage all ajax calls. Because of this architecture I can't pass a callback function to the ajax function. My workaround was a token, which should be put into the worker's answer so the frontend can determine what to do.

So my call inside the worker looks like this:

ajax({
    success_func : function(data) {
        self.postMessage({
            task : cb,
            data : data
        }
    }
});

The most horrible thing I tested was

ajax({
    success_func : (function(cb) {
        return function(data) {
            self.postMessage({
                task : this.cb,
                data : data
            });
        }.bind({cb : cb});
    })(cb),
});

But as soon as I call the worker before the first ajax call was executed, I get two answers with different data but the same cb

How can I force the function to use the cb from the corresponding call and not the last one passed to the worker?

Liam
  • 27,717
  • 28
  • 128
  • 190
Sindhara
  • 1,423
  • 13
  • 20
  • 4
    *webworker in my project which should manage all ajax calls.* why would you do this? This seems a very problematic design. This smells of an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Liam Jun 07 '18 at 13:57
  • I thought it would be a good idea to let the worker handle some (synchronous) background tasks that would block the main script. – Sindhara Jun 07 '18 at 14:08
  • 1
    AJAX isn't synchronous though? The A means **asynchronous**, **A**synchronous **J**avaScript **A**nd **X**ML. It sounds like you want to just forget about the web worker – Liam Jun 07 '18 at 15:22
  • 1
    You cannot pass a function to a webworker. – Bergi Jun 07 '18 at 15:38
  • @Liam I'm using XMLHttpRequest in synchronous mode in two requests. – Sindhara Jun 08 '18 at 15:47
  • @Bergi and that's the reason I tried the token. And the passing of the token to postMessage is the problem – Sindhara Jun 08 '18 at 15:48
  • Yeah don't do that, basically **never ever** send a synchronous http request. I'm guessing your struggling with the return type? – Liam Jun 08 '18 at 15:52
  • You need to read [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) this is your **actual problem** – Liam Jun 08 '18 at 15:52
  • @kuh-chan What token are you talking about? There's none in the code you posted – Bergi Jun 08 '18 at 16:24
  • @Bergi the token is the "cb". And there's my problem. – Sindhara Jun 11 '18 at 12:41
  • @Liam I know how to return an answer from an asynchronous call. That's not the problem and it's working. Even if I dropped the sync requests, the problem I have won't be solved. The problem is the communication between worker and (let's say) main.js. If I have a call to the worker while an ajax call is still in progress, I get back the same token (cb) two times. And not both tokens as expected. – Sindhara Jun 11 '18 at 12:47
  • @kuh-chan `cb` is a function, not a token, right? – Bergi Jun 11 '18 at 12:47

0 Answers0