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

别说谁变了你拦得住时间么 提交于 2019-12-12 05:42:36

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/14260686/how-to-get-a-deferred-handler-to-return-a-value-to-the-calling-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!