0

I would appreciate some ideas/help for a decent implementation to this problem.

I have a functionA called from different workers. The function in fact makes a AJAX request to my server (really it is the same query for any of the workers).

What I want is only the first time the functionA is invoked make the request and block all the other calls until the server request finished and return the value to all the workers.

Thanks in advance.

3
  • you could do that with a Promise Commented Jan 25, 2016 at 11:12
  • Unbind then rebind on success? Commented Jan 25, 2016 at 11:13
  • 1
    by the way, using a Promise it wouldn't be blocking, it'd just be asynchronous done right :p Commented Jan 25, 2016 at 11:24

2 Answers 2

2

Whenever you want to block in JavaScript, you're doing something wrong. To solve your problem, you'd need a means of thread synchronization. There's no such thing in JavaScript.

Your only option to do that is to ensure that your request can only be dispatched from one thread.

For example, ensure that only the main GUI thread or a specific shared worker is responsible for loading the request. The solution: All other workers will send message in order to query the request and receive message when the data is loaded.

You provided no code with your question so I am not providing code either.

Sign up to request clarification or add additional context in comments.

1 Comment

Synchronisation through a dedicated actor is still synchronisation :-)
-1
function load() {
    // make AJAX and return a promise
}

var promise; // create a promise and resolve it.
function loadBlocked() {
  return (promise = promise.then(load, load));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.