Making request synchronous

1489
9
02-24-2017 08:22 AM
Highlighted
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
Highlighted
Occasional Contributor

Hy roshni basu,

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

Regards,

Flavie

Highlighted
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

Highlighted
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; 

Highlighted
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?

Reply
0 Kudos
Highlighted
Occasional Contributor

Hi Andres, 

Check the doc for dojo/request/xhr here: 

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

Highlighted
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?

Reply
0 Kudos
Highlighted
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.

Highlighted
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.

Reply
0 Kudos
Highlighted
New Contributor III

Thanks worked like a charm.

Meghan Kulkarni

Reply
0 Kudos