Reputation: 2642
I need two return values from two different ajax calling.
I have two javascript functions :
this.getVal1= function(numberDep){
var promise = $.ajax({
url:url, //REST url
type:"GET",
dataType:"json",
contentType: "application/json"
});
return promise;
};
this.getVal2= function(){
var promise = $.ajax({
url:url, //another REST url
type:"GET",
dataType:"json",
contentType: "application/json"
});
return promise;
};
How I call these two functions :
$.when(getVal1(17),getVal2())
.done(function(_topdep,_alldep){
console.log(_topdep);
console.log(_alldep);
});
Here are the result of each console.log
: https://i.sstatic.net/IHBQx.png.
What I supposed to return from first console.log
is those 17 records, and the second console.log
is 36 records (please have a look in the above image) .
Any help would be much appreciated, thank you..
Upvotes: 1
Views: 156
Reputation: 388316
Since those are ajax promises and the ajax promises have more than 1 callback param(data, status, jqXHR) the then
callback will receive an array as the value for each callback. So to get the data you need to get the first member of each argument
$.when(getVal1(17), getVal2())
.done(function (_topdep, _alldep) {
console.log( _topdep[0] );
console.log( _alldep[0] );
});
Demo: Fiddle
Upvotes: 3
Reputation: 7138
I'm guessing you want to return the results of the two separate ajax calls after both finish? If that's the case, then you could wrap each ajax request in a Promise
and use the Promise.all([promise1, promise2])
construct.
Upvotes: -2