antonpug
antonpug

Reputation: 14296

How to get a deferred handler to return a value to the calling function?

Look at the below example to understand what I am trying to do:

//Caller.js
callingFunction : function (...)
{
    var a = new Assistant();
    console.log("This object has been returned ", a.showDialog(...));
},

//Assistant.js
showDialog : function (...)
{
    deferred.then(lang.hitch(this, this._showDialog));
    //I want to return someObject to callingFunction
},

_showDialog : function (dialogData)
{
    ...
    ...
    return someObject;
},}

Upvotes: 0

Views: 655

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074168

Since it's deferred, there is nothing for it to return before that function ends. Instead, pass a callback into showDialog and have it call that callback when the deferred fires.


Re your comment below:

Do you know how I would add a callback to that?

It's been years since I used Dojo, so it may have features to make this shorter, but the usual way would look like this:

showDialog : function (callback)
{
    deferred.then(lang.hitch(this, function() {
        this._showDialog();
        callback(/*...whatever it is you want to pass back...*/);
    }));
},

Upvotes: 2

Related Questions