Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery deferred call back oddness
    primarykey
    data
    text
    <p>I was playing around with call backs and deferred functions in jQuery and was wondering if anyone could tell me why this works</p> <p><a href="http://jsfiddle.net/austinbv/QVujr/" rel="nofollow">http://jsfiddle.net/austinbv/QVujr/</a></p> <pre><code> get_each_total = function(callback) { var requests; requests = []; var url; url = "http://otter.topsy.com/search.js?callback=?&amp;apikey=38A260E9D12A4908B1AF9184B691131&amp;q=justin+bieber&amp;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=?&amp;apikey=38A260E9D12A4908B1AF9184B691131&amp;q=justin+bieber&amp;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")); }); </code></pre> <p>and this does not</p> <p><a href="http://jsfiddle.net/austinbv/wzve6/" rel="nofollow">http://jsfiddle.net/austinbv/wzve6/</a></p> <pre><code> get_each_total = function(callback) { var requests; requests = []; var url; url = "http://otter.topsy.com/search.js?callback=?&amp;apikey=38A260E9D12A4908B1AF9184B691131&amp;q=justin+bieber&amp;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=?&amp;apikey=38A260E9D12A4908B1AF9184B691131&amp;q=justin+bieber&amp;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")}); }); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload