如何从延迟对象访问返回值? [英] How to access return value from deferred object?
问题描述
我有以下代码,在存储库中使用 $。getJSON
返回一些数据,然后由其他函数使用。
I have the following code that uses $.getJSON
inside the repository to return some data that is then used by other functions.
$.when(
repository.getUserDetails().done(dataPrimer.getUserDetails),
$.Deferred(
function (deferred) {
deferred.resolve();
}
)
).done(
function () {
repository.getUserPolicyTitles().done(dataPrimer.getUserPolicyTitles);
},
function () {
repository.getUserPage().done();
}
);
这有效但我需要从以下位置返回一个值: repository.getUserDetails( ).done(dataPrimer.getUserDetails)
可以用作参数: repository.getUserPage()。done();
This works but I need to return a value from: repository.getUserDetails().done(dataPrimer.getUserDetails)
that can be used as a parameter to: repository.getUserPage().done();
getUserDetails的dataPrimer模块目前如下所示:
The dataPrimer module for getUserDetails currently looks like this:
var getUserDetails = function (jsonString) {
var object = parser.parse(jsonString);
userDetails.userName = object.user.userName;
userDetails.lastPolicyWorkedOn = object.user.lastPolicyWorkedOn;
return userDetails.lastPolicyWorkedOn;
}
我尝试过一些东西,比如 .pipe ()
没有快乐,并且想要确信我正在使用一种不错的方法,所以我正在寻找最佳实践方法来返回参数并在<$ c $中使用它c> repository.getUserPage()函数好吗?
I have tried a few things such as .pipe()
with no joy and want to be confident that I'm using a decent approach so I'm looking for the "best practice" way to return the parameter and use it in the repository.getUserPage()
function please?
推荐答案
你应该使用然后。示例中的数据 - getUserPolicyTitles函数返回的数据。
You should use "then" . The "data" in example -- data returned by "getUserPolicyTitles" function.
$.when(
repository.getUserDetails().done(dataPrimer.getUserDetails),
$.Deferred(
function (deferred) {
deferred.resolve();
}
)
).done(function() {
repository
.getUserPolicyTitles()
.done(dataPrimer.getUserPolicyTitles)
.then(function(data) {
repository.getUserPage().done();
})
});
这篇关于如何从延迟对象访问返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!