Javascript 如何让延迟处理程序向调用函数返回值?

Javascript 如何让延迟处理程序向调用函数返回值?,javascript,dojo,deferred,Javascript,Dojo,Deferred,请看下面的示例,了解我正在尝试做什么: //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,

请看下面的示例,了解我正在尝试做什么:

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

因为它是延迟的,所以在该函数结束之前,它不会返回任何内容。相反,将回调传递到
showDialog
,并让它在延迟触发时调用该回调


下面是您的评论:


你知道我该如何添加回调吗

我已经多年没有使用Dojo了,所以它可能有一些功能可以使它变短,但通常的方式是这样的:

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

我真的不是那个能给出正确答案的人,因为我不使用Dojo,但我刚刚在这里读到另一个问题,
promise
s在这方面很好,因为它将执行回调,即使你在应该触发回调的事件已经发生之后添加它。你知道我将如何添加回调吗?@antonpug:添加了一个示例,因此无论回调返回什么,都将被返回,就好像它是一样从showDialog返回?@antonpug:将函数传递到
showDialog
<代码>显示对话框将没有返回值(如果该值的源是异步的,则不能返回)。稍后,当deferred运行时,您传递到
showDialog
的回调函数将被deferred调用,并带有您告诉它提供的任何参数。如果
this.\u showDialog
返回您想要传递给回调函数的内容,那么您需要将传递给
hitch
的匿名函数更改为
function(){callback(this.\u showDialog();}
。因此,如果有一个外部类调用this class.showDialog(),那么如何让showDialog将值返回给externalClass?