如何获取延迟处理程序以将值返回给调用函数? [英] 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;
},}
推荐答案
在该功能结束之前,没有什么可以返回的。相反,将回调传递给 showDialog
,并在延迟触发时将其调用回调。
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?
自从我使用Dojo以来,它已经是几年了,所以它可能具有使这个更短的功能,但通常的方式将会如下所示:
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...*/);
}));
},
这篇关于如何获取延迟处理程序以将值返回给调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文