How can a queryTask be executed synchronously? I'd like a feature set to be returned instead of deferred object.

  • I use queryTask.execute in a cycle because I search all features from several layers that are found inside some rectangle.So I run by layers and for each layer call queryTask.execute and in callback function in its parameter "results" I get some feature set. Because this object does not contain corresponding layer id I'd like to create a new object that contains layer id and feature set. So I need callback function work synchronously. I tried your advice but this does not help: firstly the cycle is ended and only after that the callback or onComplete or .then of deferred begin to work (also in
    – user32439
    Commented Jun 15, 2014 at 13:44

1 Answer 1


In instances like this, the API returns deferred objects in case you're interested in identifying when more than one request to a server has resolved, but its not something your application logic has to worry about if you're just dealing with one task at a time.

for example, when you call QueryTask.execute() and leverage the in-built callback, you can get a reference to the featureset output you want:

queryTask.execute(query, myCallback, myErrorBack);
myCallback(results) {
  //do something

To work with QueryTask deferreds, you have an additional option to do something like this:

var myDeferred = queryTask.execute(query);
  • Thanks, that makes sense however, I need to create a function/(web service?) that returns a feature set. The callback function performs a task, but I need to return a result, not a promise. How would you go about that?
    – Matej
    Commented May 13, 2014 at 21:00
  • sorry, but i'm not sure what youre talking about. Commented May 14, 2014 at 18:15
  • Let me try to rephrase.. If I return a result of a queryTask, I get a promise. Let's say I have a function called myFunction that performs a queryTask based on a polygon geometry. The function call would look something like: var result = myFunction(myGeometry, myMapServiceUrl); Since the function returns before the queryTask completes, the result is of promise type. Perhaps, I need to treat the result as it was an actual featureSet?
    – Matej
    Commented May 14, 2014 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.