Javascript ManualReset event


I have a JavaScript API that has a function x that takes some parameters and a callback function. Is there a way I can wait for the callback in the function that calls it almost the same way ManualResetEvent works in C#?

Best Solution

JavaScript has nothing like ManualResetEvent. In fact, JavaScript is run in a single thread by most browsers [Firefox did introduce WebWorkers but it's specific to Firefox and not in widespread use yet.].

If an API method takes a callback function argument, it will either call it during the method execution OR at the end when it is finished doing something. So there is no need for you to 'wait' yourself.

e.g. in jQuery, animate methods take callback function which is called when the animation is over.

$('#myDiv').slideUp(3000, function() { alert('I will be called after slideUp animation is over');});

If you could give little more details about your API method call and what you are trying to do, we can help you out.