Rice
Rice

Reputation: 3531

Dojo: Correct way to 'Hitch' scope of deferred types

I am experiencing difficulty passing scope through the following Dojo widget's asynchronous structure:

function callDef(){
//This function has the scope I need
        var deferred = new Deferred();
//try to hitch 'this' to function2
    DojoBaseLang.hitch(this,deferred.resolve(function2(1)));
    deferred.then(DojoBaseLang.hitch(this, function(callback) {
        callback.then(
            function (desiredResult) {
                //How to hitch callDef initial scope to function3?
                function3(desiredResult);
            },
            function (err) {
                // Do something when the process errors out
                console.log(err);
           })
           }),
    function (err) {
        // Do something when the process errors out
        console.log(err);
    }
);
function function2(variable){
//callDef scope not passed by hitch :(
    var dataStucture;
    //deferredFunction is a function which returns type Deferred
     return deferredFunction(hierarchyTableQuery, function(dataSet){
         //some iterative maniupulations will be performed on dataStructure here
         dataStructure = dataSet;

     }).then(function (){
         return dataStructure;
     });
}

function function3(variable){
    //need a way to also have scope in this method
    //doing other stuff
}

As you can see, callDef first calls into function2, returns a deferred, finishes execution and then passes the result from function2's dataStructure object into function3. This all works fine in terms of Deferred/Async behavior, the problem is that the dojo/_base/lang.hitch function being called is not passing the scope from function to function as it usually does, in this case from callDef to function2. I would also like to pass the same scope into function3. My require statement is correct, and I have other non-async .hitch calls which are successful in the same widget/file.

Thank you for any assistance

Upvotes: 0

Views: 560

Answers (1)

Bergi
Bergi

Reputation: 664356

You're probably looking for

…
deferred.resolve(DojoBaseLang.hitch(this,function2));
deferred.then(DojoBaseLang.hitch(this, function(callback) {
    callback(1).then(
        DojoBaseLang.hitch(this,function3),
        function (err) {
… // rest of the code

Upvotes: 2

Related Questions