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?