简体   繁体   中英

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;
},}

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...*/);
    }));
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM