views:

32

answers:

1

ok i have a javascript api nothing fancy 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#.

thanks

A: 

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.

SolutionYogi
The reason i ask is. I need to make a async function a sync function. Its more general no specific api inmind.
Pintac
Are you the author of the async function? If yes, then you can make it sync yourself. If your function is async because you are calling some other async function, then you don't really have much choice. In any case, posting code of what you are trying to do will be really helpful.
SolutionYogi