The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to wait for a response from 3 glideajax asynchronous calls within a client script, and then continue with the rest code?

Smith Johnson
Tera Guru

Hello,

I have a catalog client script where I make three glideajax calls (asynchronous). 

            var ga1=new GlideAjax("PUtilsAjax");
		ga1.addParam("sysparm_name", "region1");
		ga1.addParam("sysparm_vs_token", token);
		ga1.getXML(callback1);
	
	    var ga2=new GlideAjax("PUtilsAjax");
		ga2.addParam("sysparm_name", "region2");
		ga2.addParam("sysparm_vs_token", token);
		ga2.getXML(callback2);
	
	    var ga3=new GlideAjax("PUtilsAjax");
		ga3.addParam("sysparm_name", "region3");
		ga3.addParam("sysparm_vs_token", token);
		ga3.getXML(callback3);
		   
	
		function callback1(response){
			var answerStr = response.responseXML.documentElement.getAttribute("answer");
			var ans = JSON.parse(answerStr);
			console.log("answer region1");
			console.log(ans);
		}
	
		function callback2(response){
			var answerStr = response.responseXML.documentElement.getAttribute("answer");
			var ans = JSON.parse(answerStr);
			console.log("answer region2");
			console.log(ans);
		}
	
		function callback3(response){
			var answerStr = response.responseXML.documentElement.getAttribute("answer");
			var ans = JSON.parse(answerStr);
			console.log("answer rehion3");
			console.log(ans);
		}

                // I want the logic below to be executed ONLY IF I have the responses from the three asynchronous glide ajax calls

                // some logic ++


My first question is: Are these calls in reality asynchronous?? Because even if the third call lasts 3 sec and the first call lasts 20 sec, the third callback is executed after the first one. Do I miss something here?

My second question is: How I can execute some logic ONLY IF I have the responses from the three asynchronous calls??


I would be grateful for your assistance.

Smith.

7 REPLIES 7

Hitoshi Ozawa
Giga Sage
Giga Sage

Agree with Maik that ServiceNow client script aren't executing in parallel.

I'll create multiple events and have multiple script actions to handle each event.

FYI, async calls are call asynchronously but the response object from these calls are blocking making each call in serial instead of parallel.

Thank you @Hitoshi Ozawa , I didn't know that. This was helpful.

Regarding your comment with multiple events, what's the logic?

1) Can I trigger such events from client-script or I guess from script include? After the trigger of the four events, I don't want to execute the rest code. I need to take the 4 responses and aggregate them in an array to be able to continue further with the rest code. Can I do so in my script include ( I mean to wait ) ???

2) For example, I trigger 4 different events from my script include. Then I have 4 different script actions (based on the trigger of each event). Are they going to be executed in parallel?

3) When each of the script actions is completed and a response is received. Can the script action sent back to my script include the response?

Thank you in advance. Your answers might be really helpful.