First, your simplified version will not work, because you need to pass the do function instead of calling it.
function doRequest (){ $.ajax({ url: 'www.google.com.pe', success: _do }); }
But it looks like you are asking how to run some other code every time you call do .
If do is only called inside the doRequest() function, just add your other code to the anonymous function, which calls do at the right time.
function doRequest (){ $.ajax({ url: 'www.google.com.pe', success: function(response) {
If you want it to be more generalized, you can create a function decorator that returns a function that calls do after some other code.
function doFactory(fn) { return function() { fn.apply(this, arguments); _do.apply(this, arguments); } }
then perform the following functions:
var doFoo = doFactory(function() { console.log("foo"); });
If your requirement is more specific to preprocessing a response , you can redo it as follows:
function doFactory(fn) { return function(response) { _do.call(this, fn.call(this, response)); } }
Then do fn manipulate and return a response .
var doFoo = doFactory(function(response) { return response + "foo"; });
I hate lazy
source share