【问题标题】:Promises - I get the same result after $q.all承诺 - 在 $q.all 之后我得到相同的结果
【发布时间】:2016-08-13 02:23:59
【问题描述】:

我有这个代码:

return WordPress.getAllCategories()
  .then(function (cats) {
  var category = {};
  $q.all(cats.data.map(function (cat) {
    return WordPress.getLatestPostOfCategory(cat.id)
      .then(function (post) {
        return WordPress.getMediaById(post.data.featured_media)
          .then(function (media) {

            console.log('post id: ' + post.data.id);

            console.log('Post title: ' + post.data.title.rendered);

            category.post = {};
            category.post.id = post.data.id;
            category.post.title = post.data.title.rendered;
            category.post.content = post.data.content.rendered;

            var splitted = category.post.content.split('<p><!--more--></p>');
            category.post.introAsHtml = splitted[0];
            category.post.contentAsHtml = splitted[1];

            category.post.thumbnail = media.data.source_url;

            return category;
          });
      });
  })).then(function (res) {
    console.log(res);
  });
});

为杂志主页加载每个类别的最新文章(使用带有 $http 请求的 WordPress REST api)。过程如下: 1.加载所有类别。 2.获取每个类别的最新帖子。 3.获取最新帖子的媒体。 4. 根据帖子数据构建category.post 对象,并添加来自接收到的媒体的缩略图(帖子特定)。 5.所有promise解决后,$scope.categories = categories申请查看。

问题:

使用上面的代码,我可以看到控制台日志正确搜索不同的帖子和媒体,但最后我得到一个包含类别的数组,所有帖子都是相同的。相同的标题、内容、缩略图和所有内容。

我在这里的承诺做错了什么?

附:所有WordPresss 服务功能正常工作。他们通过来自 WordPress 博客的 $http 请求接收到必要的数据后,返回一个已解决的承诺。

问候。

【问题讨论】:

  • 您忘记了 return 来自 then 回调的 $q.all(…)

标签: angularjs ionic-framework promise angular-promise


【解决方案1】:

试试这个方法:

return WordPress.getAllCategories()
  .then(function (cats) {
  $q.all(cats.data.map(function (cat) {
    return WordPress.getLatestPostOfCategory(cat.id)
      .then(function (post) {
        return WordPress.getMediaById(post.data.featured_media)
          .then(function (media) {

            console.log('post id: ' + post.data.id);

            console.log('Post title: ' + post.data.title.rendered);

            var category = {}; // moved declaration here to return new instance each time
            category.post = {};
            category.post.id = post.data.id;
            category.post.title = post.data.title.rendered;
            category.post.content = post.data.content.rendered;

            var splitted = category.post.content.split('<p><!--more--></p>');
            category.post.introAsHtml = splitted[0];
            category.post.contentAsHtml = splitted[1];

            category.post.thumbnail = media.data.source_url;

            return category;
          });
      });
  })).then(function (res) {
    console.log(res);
  });
});

您返回的是相同的类别对象实例,我每次都在 getMediaById 回调中创建新实例

【讨论】:

  • 我会的。你能解释一下你在这里做了什么吗?
  • 它有效。你能编辑并解释你在那里做了什么吗?我也会将此设置为答案。非常感谢!
  • 你每次都返回相同的类别对象实例,我只是在getMediaById回调中声明它
  • 基本上只是在end block里面设置新的category?
  • 非常感谢@karaxuna!
猜你喜欢
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多