[英]Nesting promises with getJson
Not sure why this doesn't work. 不知道为什么这行不通。 I'm trying to nest some promises so that I can get the data from the playerHeader function.
我试图嵌套一些承诺,以便可以从playerHeader函数获取数据。 I understand that getJSON is asynchronous, but shouldn't the promises handle that?
我知道getJSON是异步的,但诺言不应该处理吗?
$.when(loadViewFunctions(),
$.Deferred(function(deferred) {
$(deferred.resolve);
})
).done(function() {
//remove
clearLoader();
});
function loadViewFunctions(){
$.when(playerHeader(function(app_data){
view_data = app_data;
}),
$.Deferred(function(deferred) {
$(deferred.resolve);
})
).done(function() {
console.log(view_data);
});
};
function playerHeader(){
$.when(
$.getJSON("/api/player_info.php?dev=1&key=56a26a18c775285f74c1879e26cceabb&player="+player_id+"",
function(player_data){
var player_bg = post_player_data[0].player_background_image;
append_data = "\
<div class='profile_header' id='profile-header' data-bg='"+player_bg+"'>\
</div>\
";
}),
$.Deferred(function(deferred) {
$(deferred.resolve);
})
).done(function() {
return append_data;
});
}
Is nesting promises a bad thing? 嵌套承诺是一件坏事吗?
Firstly and most importantly, I can't see any "nested" promises. 首先也是最重要的是,我看不到任何“嵌套”的承诺。 There's a single asynchronous operation
$.getJSON
that already returns a Promise - therefore, no need for $.when
/ $.Deferred
etc 有一个单一的异步操作
$.getJSON
已返回一个承诺-因此,不需要$.when
/ $.Deferred
等
It seems your question is more about Promise chaining rather than nesting 看来您的问题更多是关于Promise 链而不是嵌套
The issues in your code: 您的代码中的问题:
loadViewFunctions
doesn't return a promise for $.when
to wait on, $.when
等待时, loadViewFunctions
不会返回对$.when
的承诺$.when
playHeader
, playHeader
同样的问题, $.when
considering getJSON returns a (jQuery) promise, $.when
考虑getJSON返回(jQuery)承诺时,您也会过度使用$.when
playerHeader
passing a callback function ... which playerHeader
never calls , playerHeader
... playerHeader
从不调用, $.getJSON
callback accepts player_data
as an argument, but then tries to access post_player_data[0].player_background_image
$.getJSON
回调函数接受player_data
作为参数,但随后尝试访问post_player_data[0].player_background_image
Here are your functions with redundant code removed, notice the complete lack of $.when
这是删除了冗余代码的函数,请注意,完全没有
$.when
loadViewFunctions().then(clearLoader);
function loadViewFunctions(){
return playerHeader(function(app_data) {
view_data = app_data;
console.log(view_data);
});
};
function playerHeader(){
return $.getJSON("/api/player_info.php?dev=1&key=56a26a18c775285f74c1879e26cceabb&player="+player_id+"")
.then(function(player_data) {
var player_bg = post_player_data[0].player_background_image;
return "\
<div class='profile_header' id='profile-header' data-bg='"+player_bg+"'>\
</div>\
";
});
}
Here's the above written for modern javascript 这是上面为现代javascript编写的
loadViewFunctions().then(clearLoader);
function loadViewFunctions(){
return playerHeader().then(app_data => {
view_data = app_data;
console.log(view_data);
});
};
function playerHeader(){
return $.getJSON(`/api/player_info.php?dev=1&key=56a26a18c775285f74c1879e26cceabb&player=${player_id}`)
.then(player_data => `<div class='profile_header' id='profile-header' data-bg='${player_data[0].player_background_image}'></div>`);
}
edit: realised that
view_data
is a global, so, perhapsloadViewFunctions
isn't as redundant as I first thought编辑:意识到
view_data
是全局的,所以也许loadViewFunctions
并不像我最初想到的那样冗余
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.