AnsweredAssumed Answered

queryTask result executes after I need result

Question asked by jamesfreddyc on Apr 27, 2018
Latest reply on Apr 27, 2018 by jamesfreddyc

I'm having difficulty understanding and implementing the queryTask at startup of a widget.  That is, I need to perform a queryTask and based upon the result.features.length use that value in a conditional statement to continue to load/startup the rest of the way.

 

The problem is that queryTask.execute happens after the loading is completed and therefore my condition statement will never evaluate correctly. I know this because if you see where there are two console.log() statements, and print out in the console in this order:

 

First -- console.log("2 workplanCheck value is: ", this.workplanCheckValue)

Second -- console.log("1 workplanCheck value is: ", this.workplanCheckValue)

 

So this means that ALWAYS my this.workplanCheckValue variable doesn't get set in time before the conditional statement is evaluated!

 

constructor: function (args) {
    lang.mixin(this, args);
    window.map = this.map;
  },

startup: function () {
    ////first, check to see if a workplan boundary exists
    if (this.workplanId) {
     var queryUrl = "https://somesite/rest/services/myservice/FeatureServer/0";
     var queryTask = new QueryTask(queryUrl);
     var query = new Query();
     query.returnGeometry = true;
     query.outFields = ['*']
     query.where = "workplanId ='" + this.workplanId + "'";
     debugger;
     queryTask.execute(query, lang.hitch(this, function (result) {
        
         this.workplanCheckValue = result.features.length;
         console.log("1 workplanCheck value is: ", this.workplanCheckValue)
     }));
    }

    //use the result.features.length value to evaluate a condition and load the rest of the widget
    console.log("2 workplanCheck value is: ", this.workplanCheckValue)
    if (this.workplanCheckValue > 0 ) {
     this.beginWork_Workplan_ExistingBoundary();
    }
    else if (this.workplanCheckValue < 1) {
     this.beginWork_Workplan_NoExistingBoundary();
    }

},           

//workplan exists, zoom to it and do other stuff
beginWork_Workplan_ExistingBoundary: function(){
     ...
},
//workplan doesn't exist, zoom to something else and do other stuff
beginWork_Workplan_NoExistingBoundary: function () {
     ...
},

Outcomes