Promises and async/await for esriRequests();

02-18-2020 08:51 AM
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(


   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(



   var StipOptions={

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


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


      //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
MVP Esteemed Contributor


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

Esri Regular Contributor

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.