I am trying to associate a callback with pipes using deferred ones. Everything seems to be working fine, but in callback2 it returns me the data from callback1. This is what the code looks like:
var getCall1 = function() { return $.ajax(url, { type: "GET", data: { }, contentType: "application/json", dataType: "json" }); } var getCall2 = function () { return $.ajax(url, { type: "GET", data: {}, contentType: "application/json", dataType: "json" }); } var callback1 = function (data) { alert('call 1 completed'); }; var callback2 = function (data) { alert('call 2 completed'); }; $.when(getCall1()).done(callBack1).pipe(getCall2()).done(callBack2);
What am I missing here?
- Edit -
If I break them down into two pending calls, it works, but then why use the chain and the handset?
$.when(getCall1()).done(callBack1); $.when(getCall2()).done(callBack2);
- The right way to do this is
var getCall1 = function() { return $.ajax(url, { type: "GET", data: { }, contentType: "application/json", dataType: "json" }); }.pipe(function (d){return d}); var getCall2 = function () { return $.ajax(url, { type: "GET", data: {}, contentType: "application/json", dataType: "json" }); }.pipe(function (d){return d}); var callback1 = function (data) { alert('call 1 completed'); }; var callback2 = function (data) { alert('call 2 completed'); }; $.when(getCall1, getCall2).done(function(d1, d2){ callback1(d1); callback2(d2); });
Asdfg source share