承诺 - 我在 $q.all 之后得到相同的结果
Promises - I get the same result after $q.all
我有这个代码:
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);
});
});
为杂志主页加载每个类别的最新文章(使用 WordPress REST api 和 $http 请求)。过程如下:
1.加载所有类别。
2. 从每个类别中获取最新的 post。
3.获取最新post的媒体。
4. 基于 post 数据构建 category.post
object 并从收到的媒体 (post-specific) 添加缩略图。
5.所有promise都解决后,$scope.categories = categories
申请查看
问题:
使用上面的代码,我可以看到控制台日志正确搜索不同的 posts 和媒体,但最后我得到一个包含类别的数组,所有 posts 都是相同的。相同的标题、内容、缩略图和所有内容。
我这里的承诺哪里做错了?
P.S。所有 WordPresss
服务功能正常工作。在通过来自 WordPress 博客的 $http 请求收到必要数据后,他们 return 做出了承诺。
此致。
试试这个方法:
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
回调
中创建新实例
我有这个代码:
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);
});
});
为杂志主页加载每个类别的最新文章(使用 WordPress REST api 和 $http 请求)。过程如下:
1.加载所有类别。
2. 从每个类别中获取最新的 post。
3.获取最新post的媒体。
4. 基于 post 数据构建 category.post
object 并从收到的媒体 (post-specific) 添加缩略图。
5.所有promise都解决后,$scope.categories = categories
申请查看
问题:
使用上面的代码,我可以看到控制台日志正确搜索不同的 posts 和媒体,但最后我得到一个包含类别的数组,所有 posts 都是相同的。相同的标题、内容、缩略图和所有内容。
我这里的承诺哪里做错了?
P.S。所有 WordPresss
服务功能正常工作。在通过来自 WordPress 博客的 $http 请求收到必要数据后,他们 return 做出了承诺。
此致。
试试这个方法:
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
回调