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

Maik Skoddow
Tera Patron
Tera Patron

Hi

you cannot force parallel execution on server side triggered from client side, as ServiceNow enforces session synchronization (session sync).

That means:

  • Single active session per user pre browser
  • Only a single transaction per session can be executed at once
  • Multiple requests from the same session will run serially

Kind regards
Maik

Hi Maik,

thank you for your comment. Much appreciated.

Based on your experience, is there any other way to perform parallel execution directly in client side?
For example, I want to make 4 api calls to a 3rd system in a catalog client script, and once I have the responses from all calls, then to continue and execute the rest code of the client script. Is this possible on SN?? Or any other ways to do so?

Hi @Smith Johnson 

on client side you cannot perform AJAX based requests to 3rd party system as your browser will block these cross domain requests.

So if you really need parallel requests you have to invest a bit more. My first idea is to create a Flow, add in that Flow parallel execution gateways and perform the REST requests in respective actions. After that you should join the individual Flow paths and merge the results for sending back to client.

And on client-side, you can trigger a Flow execution via script.

Kind regards
Maik

Hi Maik,

thank you for you response.

But if I trigger a flow from my client script, then the flow is server-side, so can it make parallel calls?? Because before you mentioned that server side can to force parallel execution.

For example, trying this with a workflow (as shown below), the calls are executed sequentially and not in parallel.

find_real_file.png