1

I am trying to use $.when to execute my Ajax Request and to do manipulate data after their execution. Here is my code until now:

function method2 (data2) {
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data2){
            console.log(data2);
        }

    });
}

function method3 (){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data3){
            console.log(data3);
        }
    });
}


function method4(){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data4){
            console.log(data4);
        }
    });
}


function method5(){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data5){
            console.log(data5);
        }
    });
}

function method6(){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data6){
            console.log(data6); 
        }
    });
}



// Execute all needed data from all of the requests.
function showData(){

}

$.when( method1(), method2(), method3(), method4(), method5(), method6() ).then( showData() );

So I would like to show the data from these Ajax get requests on my HTML page and I would like to execute all of the code inside function showData() but the problem is that I do not have access to my methods when I try to console.log() them inside showData() and I would like to know how I can access them ? Any solution or ideas ?

1
  • 1
    Read the documentation and check the examples: $.when() Commented Feb 27, 2016 at 13:22

3 Answers 3

2

From the documentation with little modification:

function method1() {
  return $.ajax({
    type: 'GET',
    url: 'Some URL',
    dataType: 'jsonp'
  });
}

function method2() {
 return $.ajax({
    type: 'GET',
    url: 'Some URL',
    dataType: 'jsonp'
  });
}

// Execute all needed data from all of the requests.
function showData(v1, v2) {
  console.log(v1);
  console.log(v2);
}

$.when(method1, method2).done(showData);
Sign up to request clarification or add additional context in comments.

3 Comments

I have checked the documentation and your explanation but when i try to console.log(v2); for example inside function showData(v2){...} i receive Uncaught ReferenceError: v2 is not defined and I cannot output the data that I want to
@divkiller You don't need to use $.Deferred (and handle the resolve yourself) here. Just do return $.ajax(...) as $.ajax() already returns a Promise: fiddle
@Andreas I am going to try both ways, but yea thank you for the help and fast suggestions :) really appreciate your time !
0
function showData(){
var array = ['url1', 'url2', 'url3', 'url4', 'url5', 'url6'];
for (var i = 0; i < array.length; i++) {
      $.ajax({
        type: 'GET',
        url: array[i],
        dataType: 'jsonp',
        success: function(data){
        console.log(data)
    }
});
}
}
showData();

1 Comment

That's not what divkiller has asked for. Just read the documentation of $.when() and it should be obvious.
0

Try series from caolan/async library: https://github.com/caolan/async#seriestasks-callback

function method2 (callback) {
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data2){
            callback(null, data2); // Call callback, first parameter if there was an error, second the result
        }
    });
}

function method3 (callback){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data3){
            callback(null, data3);
        }
    });
}

function method4(callback){
    return $.ajax({
        type: 'GET',
        url: 'Some URL',
        dataType: 'jsonp',
        success: function(data4){
            callback(null, data4);
        }
    });
}

/* ... */

async.series([
    method2,
    method3,
    method4,
    /* ... */
], function(err, results){
    console.log(results); // Find all the stuff you passed to the callbacks, e.g. results = [data2, data3, data4];
});

1 Comment

There is no separate library required (besides jQuery) to achieve exactly the same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.