Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...