Making request synchronous

3532
9
02-24-2017 08:22 AM
roshnibasu
New Contributor III

Is there a way to make the esri/request synchronous?

esriRequest(url, {
responseType: "json",
callbackParamName: "callback"
}).then(function(response){
console.log(resp);
});

9 Replies
FlavieMORAUX1
Occasional Contributor

Hy roshni basu,

I ask myslef the same question. Have you found a way to do that?

Regards,

Flavie

roshnibasu
New Contributor III

Hi Flavie,

I had resolved this issue by making an API at my java code. And then call that API from my javascript making call synchronous.

Regards

Roshni

FlavieMORAUX1
Occasional Contributor

Hi Roshni,

I found another solution, I request myself with dojo class "request/xhr".

Regards,

Flavie

var layerInfo = null;

var requestURL = layerUrl + "?f=json";
requestXHR(requestURL, {
   handleAs: "json",
   sync: true,
   headers: {
      "X-Requested-With": null
   }
}).then(function(data){
   layerInfo = data;
});

return layerInfo; 

AndresCastillo
MVP Regular Contributor

Although I see you have the dojo class "request/xhr" property of  sync set to a boolean value of true, then object still has a .then method promise.

Can you help me understand how this is synchronous please?

0 Kudos
LeoLiu1
Occasional Contributor

Hi Andres, 

Check the doc for dojo/request/xhr here: 

dojo/request/xhr — The Dojo Toolkit - Reference Guide

AndresCastillo
MVP Regular Contributor

Thank you Leo.

I understand that the requestXHR function is set to sync.

But after it runs, it returns a promise, which would make it async again, right?

0 Kudos
LeoLiu1
Occasional Contributor

Hi Andres,

dojo/request/xhr uses XMLHttpRequest.open to perform the request. If 'sync' is true, the promise won't be returned until the request has completed.

AndresCastillo
MVP Regular Contributor

Thank you Leo for your replies.

I will have to do a small test on different scenarios of this to see its' effect on the application's behavior.

0 Kudos
MeghanKulkarni
New Contributor III

Thanks worked like a charm.

Meghan Kulkarni

0 Kudos