1

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;
            });
        }
    });
3
  • 1
    You cannot. You have to return the deferred and put the "then" outside execute. myClass.execute(obj).then(f(featureSet){}).
    – ca0v
    Commented May 2, 2014 at 17:24
  • @ca0v: Thank you! Could you provide a link where this is explained? Then move it to the answers please.
    – Matej
    Commented May 2, 2014 at 17:57
  • It will be intuitive once you understand deferred. Indirect docs would be for all and when.
    – ca0v
    Commented May 2, 2014 at 18:52

1 Answer 1

2

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.

0

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.