JS ArcGIS API question

858
7
04-11-2020 08:18 PM
MeghanKulkarni
New Contributor III

Hello,

I have one issue in code which I could not figure out. I'm making esriRequest function. Code details:

var req = EsriRequest({ url: lyrIdUrl, content: { f: "json" }, handleAs: "json", callbackParamName: "" },
{ usePost: true });
req.then(function (response) {
debugger;
geotypeJson = response;
alert("Succeess");
}, function (error) {
console.log(error.message);
alert("ID Error");
});
debugger;

For some reason code followed by req.then executes first before what's code inside req.then (I added debugger to confirm this). Can someone help me understand why? Also, can someone help me fix it, please? I want to assign geotypeJson to response from lyrIdUrl. 

Meghan Kulkarni#

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Meghan,

   That is expected. The code following the .then is inline code that does not wait for the promise to resolve. So if you have code that is dependent on the request result then it has to inside the .then function.

MeghanKulkarni
New Contributor III

Thanks Robert. Isn't my code in then function already?

req.then(function (response) {
debugger;
geotypeJson = response;
alert("Succeess");
}, function (error) {
console.log(error.message);
alert("ID Error");
});

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Meghan,

  So I am not sure what's the question you have then.

You will always get to the debugger statemkent outside the .then function before you get to inside the debugger (as I mentioned earlier).

0 Kudos
MeghanKulkarni
New Contributor III

Hello Robert,

Basically I want to assign geotypeJson = response; as a consequence of esriRequest execution complete. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Then you need to have anything that uses geoTypeJson wait for the .then to complete or have anything that uses it inside the .then.

MeghanKulkarni
New Contributor III

Let me ask you in a bit of a different way. Is there a way to make esriRequest synchronous from default asynchronous?

Thanks.

Meghan Kulkarni

0 Kudos
MeghanKulkarni
New Contributor III

Nevermind. I found solution in the thread

https://community.esri.com/thread/190913-making-request-synchronous 

0 Kudos