0

I have examined all of the similar questions about this issue, but still could not figure out the way it should work. Could anyone lead me in the right direction. data and data2 is not loaded on time but after I skip $.when part in chrome debugger they appear. Thanks in advance.

function First (number) {    
  return $.ajax({
        url: "/…",
        data: { 'number': number },
        type: "POST",
        cache: "False",
        success: function (data) {
            return data;
        },
        error: function (xhr, type) {
            alert('Something went wrong.')
        }
    });
}

function Second (number) {    
  return $.ajax({
        url: "/…",
        data: { 'number': number },
        type: "POST",
        cache: "False",
        success: function (data2) {
            return data2;
        },
        error: function (xhr, type) {
            alert('Something went wrong.')
        }
    });
}

function DoSomeBusiness(number) {
    $.when(First(number), Second(number)).then(function (data, data2) {
       var someData = data; // data and data2 is undefined for sometime but right after I pass this line of code they appear correctly but i cannot use them cause they’re not loading on time.
       var someData2 = data2;          
    });    

    .. do some business           
};
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
klevendoglu
  • 592
  • 1
  • 6
  • 18
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Aug 04 '17 at 12:25
  • @Liam OP is using Promise and `$.when()` so IMHO incorrect dup target – Satpal Aug 04 '17 at 12:27
  • There's a whole section in [the answer](https://stackoverflow.com/a/14220323/542251) *ES2015+: Promises with then()* @Satpal – Liam Aug 04 '17 at 12:27
  • note: success / error / complete are deprecated now, better to use done / fail / always – Kaddath Aug 04 '17 at 12:30
  • not to mention [this answer **If you're using promises, this answer is for you.**](https://stackoverflow.com/a/30180679/542251) – Liam Aug 04 '17 at 12:31
  • I take it back, they hid that away. None of the method signatures say, this is deprecated. – Liam Aug 04 '17 at 12:40
  • Please log the value of `JSON.stringify(data)` inside the `then` callback and verify whether it really is `undefined`. Of course, it will be defined at `// .. do some business`, because there you are accessing it too soon. – trincot Aug 04 '17 at 12:43
  • Move `...do some business` inside the `then` – charlietfl Aug 04 '17 at 12:45
  • @charlietfl I'm using someData and someData2 variables outside of then. so I guess if they were loading with the called data business part would also work right? – klevendoglu Aug 04 '17 at 12:48
  • They aren't available there. `$.ajax` and `$.when` are asynchronous – charlietfl Aug 04 '17 at 12:50
  • "*I'm using someData and someData2 variables outside of then*" - which goes right back to the very first comment and this being a duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Aug 04 '17 at 13:16
  • I've reformatted the code to make it clearer that "..do some business" is *not* part of the promise. You can't "do some business" with `someData` until the ajax has completed. Which also matches with your comment "undefined for sometime after this line". – freedomn-m Aug 04 '17 at 13:19
  • The reason that I had posted a new question is that I could not apply possible solutions to my code.Thanks for reformatting. – klevendoglu Aug 04 '17 at 13:22
  • If you've not understood the issue yet: it's because the `$.when()` ajax calls are asynchronous. ie the code at "..do some business.." runs *before* the code inside the `.then()`. The linked question has some more details and things to do (the most basic would be to move the "..do some bussiness..." code inside the `then()` as suggested by @charlietfl ) – freedomn-m Aug 04 '17 at 13:50
  • there might be another issue with my code cause even if I move "do some bussiness" part inside of then() it still does not work. – klevendoglu Aug 04 '17 at 14:12

1 Answers1

-1

dont use the success/error fields in the ajax call,you should manage them in the .then callbacks

edit: and the cache field should be a boolean

edit2: the question is not very clear and i thought the problem was one of those callbacks interfering with the deferred behavior because the code looks fine. Maybe OP wants to access data and data2 where he wrote

 .. do some business     

and that is an error because whatever is there gets executed before whatever is in the .then callback. So OP just move your code inside the callback and it should work