我从动作创建者处调用一个函数:

getPostComments(author, permlink) .then(comments => {...

该函数也被递归调用以获取所有嵌套的注释:

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条评论,其中一个具有嵌套评论。没有嵌套注释的注释返回注释对象,而具有嵌套注释的注释返回Promise对象。

javascript - Redux/Thunk中的递归函数返回Promise对象,而不是常规对象-LMLPHP

javascript - Redux/Thunk中的递归函数返回Promise对象,而不是常规对象-LMLPHP

0:是带有评论的评论。
1:仅发表评论。
2:仅发表评论。

因为我对它执行了.then(unpromised => {来解析对getPostComments的递归调用,所以其中的嵌套注释(0 :)作为常规对象返回。

javascript - Redux/Thunk中的递归函数返回Promise对象,而不是常规对象-LMLPHP

但是第一次调用getPostComments是Promise对象(如前两幅图像所示),我想要常规数据对象,而不是Promise对象。

如何使其成为常规对象?怎么了?

谢谢!

最佳答案

看来您的问题只是您没有等待map的承诺解决。 Promise.all应该修复它,我认为(未经测试)。

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;
      }
    })));
}

07-24 09:44
查看更多