Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

javascript - 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;
},}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...