Promises and async/await for esriRequests();

1788
2
02-18-2020 08:51 AM
MichaelThornton2
New Contributor

I have some code which has several nested fetch api promises as such:

require({"esri/request}"], function(esriRequest){

   var options ={

      //some options

};

var url = 'SomeParcelGeometryRestService'

esriRequest(url, options).then(

function(response){

   var LLDOptions ={

         //more options 

         //takes geometry of 'response' from Parcel geometry request to run an intersect on the LegalLandDesc RS url

   };

   esriRequest('aLegal Land Desc RS url', newOptions).then(

    function(LegLandResponse){

   });

   var StipOptions={

         //takes the geometry from the first Parcel geo request for intersect with Stip url

   };

   esriRequest('Stipulations Rest Service Url', StipOptions).then(

   function(StipResponse){

      //write a pdf report using jsPDF after going to an AJAX call to a SQL esri db. and returning results from the objects resovled in the LLD request and the Stip Request which I am currently still embedded in

   });

});

The issue here is that the Stipulation Intersect Request is on the same level as the LLD Intersect Request, and both use the geometry from the first Parcel EsriRequest. So they are both embedded in that Parcel Geo Get Request, but not embedded within each other, and yet I am using an array or an object (either way) from the LLD request in the Stip request and need to wait till it is not, but it really does not work to embed it in the LLD response. 

My thoughts are I could chain them , or use an async/await method to tell StipRequest to wait on the LLD request.

Anyone know how to do this? Do I need to put the Stip request in a seperate function and call that function with .then after LLD request? 

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Michael,

   You can use the dojo promise all. It was designed for exactly this situation

by Anonymous User
Not applicable

Hi Michael Thornton

Just add a few example for you.

esriRequest(url, options) return deffered object.

To wait lld and stip request to finish,

var promiseArr = [];

promiseArr.push(esriRequest('aLegal Land Desc RS url', newOptions));// add your lld request deferr into array

promiseArr.push(esriRequest('Stipulations Rest Service Url', StipOptions));

 all(promiseArr).then(function(results){    // results will be an Array  });

Make sure you import ["dojo/promise/all"] as well.