This is a newbie question: I have a pre-existing function that I would like to have call another function when it is finished, however, it does not accept a callback nor of course call one. I can modify this code to accept and call a function however this got me thinking about whether JavaScript supports doing this ... I would think it does but I've never had reason to find this out, I'm assuming it's necessary when working with libraries where we cannot change the code to our liking. Thanks.
-
Do you have a example to clarify your question? JavaScript is a general purpose language, a lot of things are possible.FloydThreepwood– FloydThreepwood2011-11-08 22:06:54 +00:00Commented Nov 8, 2011 at 22:06
-
Why can't you just call the two functions one after the other?David Schwartz– David Schwartz2011-11-08 22:07:47 +00:00Commented Nov 8, 2011 at 22:07
-
I want function B to fire when function A has completed -- function A is making a request to a remote server and function B depends upon changes that function A has made to a global variable.bethesdaboys– bethesdaboys2011-11-08 22:10:53 +00:00Commented Nov 8, 2011 at 22:10
-
@bethesdaboys Don't worry about libraries in this case, they almost always give you a standart way to intercept any such call.FloydThreepwood– FloydThreepwood2011-11-08 22:14:36 +00:00Commented Nov 8, 2011 at 22:14
4 Answers
The only time you need a callback is when you are doing something asynchronous, such as:
- making an HTTP request (and waiting for a response)
- animating something, one frame every time period until it is done
- waiting for the user to click a button
All of these are considered "done" when something happens, but there is no generic way to determine when the something has happened. You need something custom for each one.
If you aren't waiting for something, then you can just call one function after the other (foo();bar();) and not need to fiddle around with callbacks.
So…
It might be possible to do what you want, but we can't tell you a generic way to achieve it.
6 Comments
This is a bit of a hack, and i'm sure there's tidier ways to do this with polymorphism, but you can treat the function as a variable and re-assign it somewhat:
Say you start with this function:
function originalFunctionName()
{
// do something
}
you can assign the existing function to a new name:
var backupOfOriginal = originalFunction;
then you can define a new function over the original name:
var originalFunctionName = function()
{
// do something else
// call backup function
backupOfOriginal();
}
then if you call the original function name:
originalFunctionName();
all the code will execute.
Comments
You can always create a new function which calls that function and provides you with opportunities to do something else before and after it is called. Underscore.js provides .wrap() to do exactly that sort of thing and return you a function you can call instead of the first function.
That just gives you a new function to call instead of your original function, if you want every spot that called the original function to get the new behavior instead, you could take advantage of JavaScript's prototypal inheritance to replace the original function with your new version of it.
Comments
Create a wrapper function that calls the original function, then one you pass in.
If the original function is an Ajax call and you're trying to replace one of its handlers, that's a different issue, though you might be able to use jQuery's $.when(original).then(function () { ... }) depending on your actual needs.