AnsweredAssumed Answered

Looking for suggestions on recursion with promises

Question asked by ibroad on Sep 18, 2017
Latest reply on Sep 18, 2017 by rscheitlin

I have a web map that is based on data that a coworker is collecting from inspections in the field. The web map is a viewer for this data, and provides some statistics, such as how many structures are left to inspect.


I want the map to zoom into the previous days work, and if there's nothing for the previous day, then I want it to keep going back until it finds data that was inspected.


I have it working for just the previous day, but I'm not sure the best way to keep having it check the previous days until it finds data.


Here's what I currently have:

view.then(function () {

     var getYesterdayInspected_QT = new QueryTask({
          url: server + url + "21"

     var getYesterdayInspected_Q = new Query();
     getYesterdayInspected_Q.where = "InspectionDate >= date'" + yesterday + "'";
     getYesterdayInspected_Q.outFields = ["*"];

     getYesterdayInspected_QT.execute(getYesterdayInspected_Q).then(function (results) {

          var stations = [];
          for (var i = 0; i < results.features.length; i++) {

          var where = "gs_facility_id IN (";
          for (var x = 0; x < stations.length; x++) {
               where += "'" + stations[x] + "'" + ",";
          where = where.replace(/,\s*$/, "");
          where += ")";

          var getInspectedExtent_QT = new QueryTask({
               url: server + url + "2"

          var getInspectedExtent_Q = new Query();
          getInspectedExtent_Q.where = where;

          getInspectedExtent_QT.executeForExtent(getInspectedExtent_Q).then(function (results) {
               view.extent = results.extent.expand(2);



But I'm not sure of the best way to recursively find the most recent date that has data using promises? So, if it didn't find data for yesterday, it would check the day before that, etc.


Without using promises I would just call the same callback function.


Thanks for any tips/suggestions.