1

I have script following:

var price_option_oc = 0;
// Example
jQuery.ajax({
    type: "POST",
    processData: false,
    dataType: "text",
    url: mainURL + url_ajax_get_prices_oc,
    data: JSON.stringify(datas),
    contentType: "application/json; charset=utf-8",
}).done(function(data) {
    console.log('{1}=' + data);
    price_option_oc = data;
});

console.log('{2}' + price_option_oc);

Why result is, I dont know why, please explain for me

{2}0
{2}0
{1}=9
{1}=9

Why not is:

{1}=9
{1}=9
{2}0
{2}0

Thanks.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
phinq
  • 131
  • 1
  • 1
  • 12
  • Because you are doing an asynchronous call. Just set `async:false`, then it will behave like your expectation. But this is a bad way of handling this issue. – Rajaprabhu Aravindasamy Jul 15 '16 at 09:44
  • 1
    ajax requests are asynchronous, while the request is running the code will continue running `console.log('{2}' + price_option_oc)` and when the request will complete it will call the callback – WalksAway Jul 15 '16 at 09:45
  • Thanks, so, now i need add async:true to my code? – phinq Jul 15 '16 at 09:47
  • ajax is called to server first ,in that mean time, outside of ajax console is runs first – Jack jdeoel Jul 15 '16 at 09:47
  • Thank you. [link](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). I will check again. It is ok – phinq Jul 15 '16 at 09:55

1 Answers1

0

Ajax requests run asynchronously to the rest of your code. That is, they run in parallel.

So it's very likely that

console.log('{2}'+price_option_oc);

is running before the ajax call completes and runs console.log('{1}='+data);

I say "very likely" because, since the ajax call relies on external factors like network speed and the response speed of the remote server, you can't guarantee how fast it will run, and therefore you can't guarantee the order things will run in.

If you need to run code which depends on the response from an ajax call, then you must put that code into your .done function (or into a function which is called from there).

P.S. I would not use async:false to solve this type of issue - this will cause the browser to stop responding to any user input until the ajax call completes (or fails). If the call takes longer than normal, it will look to the user like their browser has crashed. This is not user-friendly. Most coders don't use this feature for that reason.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thanks, I have read "Possible duplicate of How do I return the response from an asynchronous call? – Andreas 2 mins ago". It is ok – phinq Jul 15 '16 at 09:53
  • Try to set async: false in ajax({}). – Davinder Kumar Jul 15 '16 at 10:20
  • @DavinderKumar have you read my section (right above your comment) on why `async:false` is bad? The answer that this question has been deemed a duplicate of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call also has a long and even better explanation of why `async:false` is almost always a terrible idea – ADyson Jul 15 '16 at 10:58