Select to view content in your preferred language

queryTask result executes after I need result

331
2
Jump to solution
04-27-2018 01:15 PM
JamesCrandall
MVP Frequent Contributor

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 () {
     ...
},‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This is why queryTask.execute returns a Deferred. This means it takes a little time to send the request and get the results returned. Meanwhile, the code keeps progressing. What you have to do is move lines 25-30 to inside the queryTask's execute function.

     queryTask.execute(query, lang.hitch(this, function (result) {
         
         this.workplanCheckValue = result.features.length;
         console.log("1 workplanCheck value is: ", this.workplanCheckValue)
         if (this.workplanCheckValue > 0 ) {
           this.beginWork_Workplan_ExistingBoundary();
         }
         else if (this.workplanCheckValue < 1) {
           this.beginWork_Workplan_NoExistingBoundary();
         }
     }));
    }

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

This is why queryTask.execute returns a Deferred. This means it takes a little time to send the request and get the results returned. Meanwhile, the code keeps progressing. What you have to do is move lines 25-30 to inside the queryTask's execute function.

     queryTask.execute(query, lang.hitch(this, function (result) {
         
         this.workplanCheckValue = result.features.length;
         console.log("1 workplanCheck value is: ", this.workplanCheckValue)
         if (this.workplanCheckValue > 0 ) {
           this.beginWork_Workplan_ExistingBoundary();
         }
         else if (this.workplanCheckValue < 1) {
           this.beginWork_Workplan_NoExistingBoundary();
         }
     }));
    }
JamesCrandall
MVP Frequent Contributor

Re-mapping my thought process isn't going to be as easy as I thought.

Thanks Ken!

0 Kudos