-1

i learning JS a few months, i has written simple scripts. But now i collide with a problem which i can't formulate in Google. I have the program

var iWantValueFromAjax = "im empty";
$.ajax({
  type: "POST",
  dataType:'jsonp',
  url: "https://api.novaposhta.ua/v2.0/json/",
  data: {
    "modelName": "Address",
    "calledMethod": "getCities",
    "methodProperties":{}, 
    "apiKey": "6f94a6391cb5134ee68ddb7924de2a3d"},
    success: function(msg){
      iWantValueFromAjax = msg.data.map(function(e) {
        return (e.Description);
      });
    },
  }
);
console.log(iWantValueFromAjax);

I see what value variable "iWantValueFromAjax" is not changed. I suspect that i don't understand issue jquery's scope. May be i don't understand idea scope whole, lol.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Ogurchik
  • 75
  • 7
  • Wow, don't use return-obj as a variable name, it is confusing – Norbert Huurnink Aug 17 '16 at 17:01
  • 1
    In fact, dashes are invalid as variable names; this code won't run at all as shown here. – Aurora0001 Aug 17 '16 at 17:01
  • You return something with: return varName; – Norbert Huurnink Aug 17 '16 at 17:02
  • It is not clear what is your problem. But, if you actually are trying to return something in the success callback of the $.ajax call, you can't use the returned value to do nothing, and you shouldn't trying to return anything in such a function -or in any asynchronous callback function-. You can use the code inside the function to manipulate bigger scope variables or make function calls, but you can't profit its return value. – Sergeon Aug 17 '16 at 17:07
  • @Sergeon, so i must use global variable? – Ogurchik Aug 17 '16 at 17:30
  • Well, it depends actually upon what you intent to do with the response: maybe you need to push the response to a global variable or just need to call a function. Maybe you could update your question with the actual problem you want to solve though the ajax call and why it isn't working. – Sergeon Aug 17 '16 at 18:19
  • @Sergeon, i did update question. I hope i did that clear. Thanks very mush for your comment. – Ogurchik Aug 17 '16 at 19:20
  • Side note, looks like you're also missing a `}` for the end of the `$.ajax` options. – Jacob Aug 17 '16 at 19:21

1 Answers1

0

You need to understand the nature of asynchronous requests.

Asynchronous means: You are initiating some action that (after some time) will change a state and call a callback function.

But: The normal program flow will continue after you initiated the asynchronous action.

What does it mean?

  1. The code console.log(iWantValueFromAjax) will execute right after the $.ajax() has been called. So it will definitely run before the request callback has been called
  2. Every follow-up action that depends on the result of an asynchronous call will have to be executed in the callback or called from that callback (e.g. by calling a function). Especially you cannot return anything from an asynchronous callback (technicall you can, but the return value will end up "nowhere")
devnull69
  • 16,402
  • 8
  • 50
  • 61