How to JS / jQuery code run sequentially ?

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
I have some JS code below:

JavaScript:
var outside = 0; 

$.get("demo_test.asp", function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
    outside = data
  });

outside = outside + 1;

// more calculations
Two questions :

1. How do I get access to the data variable outside of the get function ? The variable outside is not being assigned.
2. How can I (or should I, am I thinking about this wrong ?) wait for the .get() block to finish processing before moving on to my outside = outside + 1; assignment ?

I remember vaguely this has something to do with the non-blocking nature of JS. Right now all of my calculations code is living inside the .get() block...which is fine until I need to call another .get()
 

purbeast0

No Lifer
Sep 13, 2001
51,858
4,366
126
1. You don't get access to the data variable outside. The only way to do that would be to do what you are doing by setting a variable on the outter scope to it. The variable outside is being assigned. If you were to put a log statement after it's being set to data, you would see that. However in your case there, the "outside = outside + 1" is being called before it's getting assigned to data since you are assigning it to data after the get call.

2. @Ken g6 already hit this on the head. You are actually already using them in your code and you don't even realize it. The jquery get method uses the promise interface as is.

 
Thread starter Similar threads Forum Replies Date
Danish Zubair Programming 8
Similar threads
Visual Studio Code

ASK THE COMMUNITY