Function Order Execution - Help needed

Hi! Hope someone can help me with this small problem.

I have two functions getData() and createStreamer() I want to execute createStreamer() after getData() has finished.

function getData(){
			
 for(var i=0;i<arrNames.length;i++){

	$.getJSON("https://wind-bow.glitch.me/twitch-api/streams/"+arrNames[i]+"", function(result){	
						
	onlineUsers.push(JSON.stringify(result.stream));	
							
	gamers.push(result);
			});			
		};
	};
function getData(){
			
			for(var i=0;i<arrNames.length;i++){

				$.getJSON("https://wind-bow.glitch.me/twitch-api/streams/"+arrNames[i]+"", function(result){	
						
					onlineUsers.push(JSON.stringify(result.stream));	
							
					gamers.push(result);
				});			
			};
	};
				
		
	function createStreamers(){
		for(var i=0;i<arrNames.length;i++){
			if(onlineUsers[i]=="null"){
				streams.push(new streamer(arrNames[i],"offline");
			}
			else{
				streams.push(new streamer(gamers[i].stream.channel.name,gamers[i].stream.channel.game);
			}
		}
	}

Hello fmtabbara,

Since you’re making multiple asynchronous queries you can’t just use the callback function in the getJSON call, but you could try to set up a callback with jQuery’s ajaxStop (api.jquery.com/ajaxStop) function which will be called once all ajax requests has finished.

Thank you!

That function works perfectly!

Many thanks

Fouad

There are other more sophisticated ways to semantically express the order in which your functions are to be executed.

You will use native javascript API called Promise. Here is an example of how you’d implement your code in Promise way.

First, make your getData function return a promise. You will implement it in such a way that it will resolve when your $.getJSON succeeds. And it will resolve itself with the data you receive from your $.getJSON

Since you’re making your $.getJSON calls in a loop, you will use Promise.all to identify if all your async calls are resolved.

function getData(){
  return new Promise((resolve, reject) => {
    let getJSONCalls = []; // this array will store all the promise states ($.getJSON calls)
    for(var i=0;i<arrNames.length;i++){
      getJSONCalls.push($.getJSON("https://wind-bow.glitch.me/twitch-api/streams/"+arrNames[i]));
  	}
  	//now once all the calls are returned, we want to resolve this getData promise
  	
  	Promise.all(getJSONCalls)
  	.then( data => {
  	  resolve(data);
  	})
  	.catch(error => {
  	  reject(error);
  	});
  	
  	//for more concise implemntation you can do the above like
  	//Promise.all(getJSONCalls).then(resolve, reject);
  })
};

Once you’re sure all the promises are resolved (using Promise.all(...).then...) you will call your createSteamers function. Also note that the createStreamers will receive onlineUsers as argument.
So you will modify your createStreamers function like

function createStreamers(onlineUsers){ // NOTE: onlineUsers is an argument to this function
	for(var i=0;i<arrNames.length;i++){
		if(onlineUsers[i]=="null"){
			streams.push(new streamer(arrNames[i],"offline"));
		} else{
			streams.push(new streamer(gamers[i].stream.channel.name,gamers[i].stream.channel.game));
		}
	}
}

And then this is how you will specify order in which your functions are to be called

getData()
.then(createStreamers)
.catch(err => console.log(err));

Read more about Promise API

1 Like

This is best way to screw up truly understanding async nature of javascript.

I’ve only been learning JavaScript for about 2-3 months now. I’m not sure I’m quite at that level yet. But thank you for the additional information.

The ajaxStop method seems to work fine and it was very simple to implement.

Just a word of advice. Something works doesn’t necessarily mean it’s right. :slight_smile:

I’d recommend you learn more about Asynchronous nature of javascript.
Since you’re 2-3 months in, I believe it is perfect time for you to begin understanding these concepts. Otherwise, you’d end up being one of those people I run into interviews who thinkthey understand javascript but really don’t. (Trust me there are plenty of those)

All the best!

Thanks for the advice!