Javascript Redux/Thunk中的递归函数返回承诺对象,而不是常规对象
我正在从动作创建者调用一个函数:Javascript Redux/Thunk中的递归函数返回承诺对象,而不是常规对象,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正在从动作创建者调用一个函数: getPostComments(作者,permlink) 。然后(注释=>{… 该函数也被递归调用以获取所有嵌套注释: const getPostComments = (author, permlink) => { return client.database.call('get_content_replies', [author, permlink]) .then(replies => { return replies.ma
getPostComments(作者,permlink)
。然后(注释=>{…
该函数也被递归调用以获取所有嵌套注释:
const getPostComments = (author, permlink) => {
return client.database.call('get_content_replies', [author, permlink])
.then(replies => {
return replies.map(r => {
if (r.children > 0) {
return getPostComments(r.author, r.permlink)
.then(children => children)
.then(unpromised => {
r.replies = unpromised
return r;
})
}else {
return r;
}
});
});
}
问题是,第一次调用返回的是Promise对象。例如,一个页面有3条注释,其中一条有嵌套注释。没有嵌套注释的注释返回comment对象,而有嵌套注释的注释返回Promise对象
0:是带有注释的注释。
1:这只是一个评论。
2:这只是一个评论
其中的嵌套注释(0:)作为常规对象返回,因为我对其执行了。然后(unmised=>{
)来解析对getPostComments
的递归调用
但是对getPostComments
的第一个调用是一个Promise对象(如前两幅图所示),我想要的是常规数据对象,而不是Promise对象
我怎样才能使它成为一个常规对象?出了什么问题
谢谢!看起来您的问题只是您没有等待
地图的承诺来解决。承诺。我认为所有的都应该解决它(未经测试)
看起来您的问题只是您没有等待映射
中的承诺来解决。承诺。我认为所有
都应该解决它(未经测试)
太好了!谢谢你帮了这个新手!谢谢你帮了这个新手
const getPostComments = (author, permlink) => {
return client.database.call('get_content_replies', [author, permlink])
.then(replies => Promise.all(replies.map(r => {
if (r.children > 0) {
return getPostComments(r.author, r.permlink)
.then(comments => {
r.replies = comments
return r;
})
} else {
return r;
}
})));
}