-2

Yes, I am aware that there are many, many, many posts about Javascript variables and how to properly access them and their scope, etc. However, I still can't figure out my problem, even after spending a couple hours trying to fix it with multiple StackOverflow posts and other articles. Here is my code, for let's say part2.js, so far:

var dataval = "";

callCgiFile().then(function(data) {
dataval = data;                          
alert(dataval);       //ALERTS data PROPERLY                                                                              
});                                                                                               
alert(dataval);         //DOESNT ALERT data at all    

I basically have another Javascript file, say part1.js, such is as follows:

function callCgiFile() {
return $.get('somecgifile.cgi');
}

and am calling that function within my part2.js file, as is shown. I am accessing the data from the CGI file and am trying to access it outside of the function, however, it is not working at all. I have honestly read many other similar questions about this problem but none of them have worked out for me. Is there anything I can do to fix my issue?? Please let me know! Thank you so much!

guest271314
  • 1
  • 10
  • 82
  • 156
  • _"am trying to access it outside of the function"_ `dataval` at second `alert(dataval);` called outside of `.then` ?, may not be defined when called ? See http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – guest271314 Aug 01 '15 at 00:13
  • My issue is I am already using the asynchronous thing for my part1.js and then calling on that in part2.js. But I suppose I also have to make my part2.js adaptable to asynchronous. I tried wrapping all of part2.js inside a function but that didn't work. I looked at that link hours back and it still didn't help unfortunately :( – yoyowasgood Aug 01 '15 at 00:16
  • Is expected result of _"am trying to access it outside of the function"_ second `alert(dataval);` ? Are "part1.js" , "part2.js" loaded into document separately ? – guest271314 Aug 01 '15 at 00:17
  • Well the CGI file returns a huge string. So when I do my first alert, that whole string does get returned. However, in my second alert, the string is not being shown at all. Yes, in the HTML file, both of them are being loaded separately – yoyowasgood Aug 01 '15 at 00:26

2 Answers2

0

The second alert will fire before the first one. The reason here is not variable scope it is the async nature of JavaScript. restructure your code so that you adapt to work async way.

  • I understand the whole asynchronous process of Ajax. However, every single solution has not worked for my code for some reason. I don't know if it is because mines is not an immediate function, but for some reason I just cannot access it after, even after trying almost every possible way to go about this problem. – yoyowasgood Aug 01 '15 at 00:27
  • @yoyowasgood: No, you **think** you understand the asynchronous stuff but if you still insist on doing it your way it means you definitely don't understand the asynchronous stuff. You can access it after but your timing is wrong you need to access it AFTER not after. Your definition of after is "after calling the function" but my definition of AFTER is "after the asynchronous function complete processing". It's not a syntax problem, it's a problem of time. Stop trying to do what you want to do and just do it the way the examples tell you how to do. – slebetman Aug 01 '15 at 01:48
  • Lol except the examples solutions didnt work...if they did, chances are I wouldn't be posting here to hear cocky people like you telling me what i know and what I don't know. – yoyowasgood Aug 01 '15 at 02:09
0

Try returning dataval at first .then() , adding second .then() to process dataval within

var dataval = "";

function callCgiFile() {
  return $.get("https://gist.githubusercontent.com/guest271314/"
               + "6a76aa9d2921350c9d53/raw/"
               + "1864137bfd2eb62705bd0e71175048a28b3253e6/abc.txt");
}

callCgiFile()
.then(function(data) {
    dataval = data;
    alert(dataval); //ALERTS data PROPERLY
    // return `dataval` at first `.then`
    return dataval
})
// do stuff with `dataval` within second `.then`
.then(function(data) {
  alert(dataval)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
guest271314
  • 1
  • 10
  • 82
  • 156
  • I'm trying to access datable outside that callCgiFile() call. Your code works, but not exactly what i'm looking for. Thanks though! – yoyowasgood Aug 01 '15 at 00:42
  • _"I'm trying to access datable outside that callCgiFile() call."_ Yes, `callCgiFile()` returns result asynchronously . What is expected result ? – guest271314 Aug 01 '15 at 00:47
  • What I meant was that I was trying to access this dataval variable outside of the callCgiFile() function as a whole. So after the });, hypothetically, I'd be able to access that dataval variable. I already tried calling callCgiFile() after the }); but that didn't do anything @guest271314 – yoyowasgood Aug 01 '15 at 05:25
  • _"trying to access this dataval variable outside of the callCgiFile() function as a whole. So after the });, hypothetically, I'd be able to access that dataval variable."_ `callCgiFile()` returns results _asynchronously_ ; the second `alert(dataval)` at `js` at Question appear to be _synchronous_ . The closing bracket , parenthesis does not automatically indicate that results, if any, have been returned as a response from `$.get()` , where `dataval` could still be `undefined` when second `alert(dataval)` is called. As a workaround, could utilize `.then()` to access `dataval` within async chain – guest271314 Aug 01 '15 at 13:52