Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd need a function to wait until the asynchronous task has completed and then return.

In my example, I'm getting undefined returned from the "execute" method:

var result = myClass.execute(obj);  //undefined

How can I get the actual result returned?

return declare(null, {
        constructor: function (obj) {
            this.mapServiceUrl = obj.mapServiceUrl;
            this.geometry_json = obj.geometry_json || undefined;
            this.where = obj.where || "1=1";

        execute: function () {
            var queryTask = new QueryTask(this.mapServiceUrl),
                query = new Query()

            query.returnGeometry = true;
            query.outFields = ["RDUWI"];

            if (this.geometry_json) {
                query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
                query.geometry = Polygon(this.geometry_json);

            query.where = this.where;
            var deferred = queryTask.execute(query);
            deferred.then(function (featureSet) {
                return featureSet;
share|improve this question
You cannot. You have to return the deferred and put the "then" outside execute. myClass.execute(obj).then(f(featureSet){}). – ca0v May 2 '14 at 17:24
@ca0v: Thank you! Could you provide a link where this is explained? Then move it to the answers please. – Matej May 2 '14 at 17:57
It will be intuitive once you understand deferred. Indirect docs would be for all and when. – ca0v May 2 '14 at 18:52
up vote 1 down vote accepted

What you want is the c# await keyword, which does not exist in javascript:

The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work. The asynchronous method in which await is used must be modified by the async keyword. Such a method, defined by using the async modifier, and usually containing one or more await expressions, is referred to as an async method.

The closest you can get to making this type of code transparent is to use when. Docs state:

dojo/when is designed to make it easier to merge coding of synchronous and asynchronous threads. Accepts promises but also transparently handles non-promises. If no callbacks are provided returns a promise, regardless of the initial value. Also, foreign promises are converted.

If callbacks are provided and the initial value is not a promise, the callback is executed immediately with no error handling. Returns a promise if the initial value is a promise, or the result of the callback otherwise.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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