0

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.

4
  • Do you have a example to clarify your question? JavaScript is a general purpose language, a lot of things are possible. Commented Nov 8, 2011 at 22:06
  • Why can't you just call the two functions one after the other? Commented 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. Commented 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. Commented Nov 8, 2011 at 22:14

4 Answers 4

1

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.

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

6 Comments

There are a lot of good reasons to use Callbacks / Closures. Event-Driven design, Functional Programming...
See my clarifying comment above, thanks. Yes, this is the first of the three scenarios you mention.
I agree; callbacks are handy in synchronous environments too.
My point is: If you are programming in JS 'fiddling' with callbacks should be second nature.
My point is: You need to know when the call the callback and dynamically making existing code do that ranges from "a slightly fiddly wrapper" to "impossible".
|
1

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

1

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

0

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.

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.