I was playing around with call backs and deferred functions in jQuery and was wondering if anyone could tell me why this works
http://jsfiddle.net/austinbv/QVujr/
get_each_total = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
get_each_total_broken = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
$(function () {
get_each_total(alert("success"));
get_each_total_broken(alert("fail"));
});
and this does not
http://jsfiddle.net/austinbv/wzve6/
get_each_total = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/search.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
get_each_total_broken = function(callback) {
var requests;
requests = [];
var url;
url = "http://otter.topsy.com/hjhkl/sehjkhhkjhkarch.js?callback=?&apikey=38A260E9D12A4908B1AF9184B691131&q=justin+bieber&window=d";
return requests.push($.getJSON(url, function(data) {
}));
return $.when.apply($, requests).then(function() {
callback();
}, function() {
return alert("There was an error communicating with a remote library, try again in a few");
});
};
$(function () {
get_each_total(function () { alert("success")});
get_each_total_broken(function () {alert("fail")});
});
as you can see the only difference is in the last two lines, where an anonymous function wraps the callback. Any insight would be nice.
here you have the
function (){ alert ....
that returns a function objecthere
alert("success")
is an object of whatever typealert()
returns and it is not a function.Edit:- in respone to the comment i'm going to further clarify.
when the browser sees
it goes throught the following steps:
is_it_complete
and call it argumentsarg1
andarg2
.do_something
and call it with the result of the above as an argument.When the browser sees:
it does:
is_it_complete(arg1, arg2)
do_something
and call it with the above object as an argument.Edit 3:- Ok so in the first one it called the alert before running your code so it appeared to work.
you have:
the second return is never reached.
Edit:- thought i would put a few tips after reading the code:
is redundant change that style to
When you do not pass any arguments to a callback do no wrap it.
it is the same as:
and as mentioned in the comments your url
Should be
or even:
There are 2 problems with your code:
1) In the last two lines of code: The first example is passing the result of an execution of the "alert" method to both the "get_each_total" and "get_each_total_broken" methods. The second example is passing a function that when executed will call "alert". The second example is the correct method to perform.
For Example:
2) You have 2 "return" statements in both your methods. The first "return" encountered within a method will return it's value and exit the method; thus resulting in the second "return" statement to never be executed. Consiquentially, the call to "$.when.apply" in both methods will never, ever get executed.
This piece of code:
exits out of your function. The
callback
is never called.P.S. When you're saying
the only difference is an anonymous function wrapping the callback
, you imply that you're also passing a function in the first version of your code. That is not true; you're trying to pass in whateveralert('whatever');
is returning, which isundefined
!Further explanation:
Both of your functions (
get_each_total
, &get_each_total_broken
) expect the parameter to be a function. This is evident by you trying to call it as a function later on in your code (callback()
). However, this line:does not pass a function to
get_each_total
. It is equivalent to the following:So, basically, you're not passing anything into your
get_each_total
function. You get asuccess
alert right away, beforeget_each_total
has been called.Neither example works. (Hint to reviewers: it is the last two lines of which he speaks.)
In your first example, you call
alert()
, which raises the alert dialog, even before theget_each_total()
function is called. Javascript invokes it, showing the alert (and giving the illusion than something happened), and passes the result of thealert()
call toget_each_total()
(and does the same with the alert in the following function, too). The result is null. If the callback were ever called, this would raise an error.In the second example, you're declaring a function and passing a reference to it to
get_each_total()
, which can then be called via yourcallback()
expression. A function is something that responds to the()
operator, meaning "do this." It would actually do something (show the alert) if your code succeeded.But your code does not. Going to the fiddle, and looking at the console, I see this message: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)."
getJSON()
never triggers the callback because it never succeeds.[EDIT] @austinbv pointed out to me that I'd missed something. The 500 code is a deliberate test on his part. But his code is still broken in
get_each_total_broken()
; there is a misplacedreturn
in the lineThis returns immediately from
get_each_total_broken
. Thewhen().then()
clause is never invoked, so he never sees the error handling. The immediacy of the alerts in the first example continues to give the illusion that something happened.