我正在创建一个延迟加载图像的函数,当需要它们时(使用分页将大数据集划分为更小的块)。
问题是承诺嵌套在items[i].imagedata
。
返回的items
数组仍然包含Promise对象而不是加载的图像。这可能是因为我使用了items.map()
,它创建了数组的副本。
function getItemImages(items, paging, cb) {
var Promise = promise.Promise;
console.log("START",items,paging);
for (var i=paging.pageOffset; i<Math.min(paging.pageOffset+paging.pageLimit,items.length); i++) {
if (!items[i].hasOwnProperty("imagedata")) {
console.log("LOADING "+i+":",items[i]);
items[i].imagedata = mongodbService.getItemImage(items[i]._id);
}
}
Promise.all(items.map((item) => {
return Promise.all([item.imagedata]);
})).then((images) => {
console.log("RESULT",paging, items);
cb(paging, items);
});
}
答案 0 :(得分:1)
除非您在某个需要回调参数的地方完成遗留接口,否则最好在最后返回Promise.all(promises),以便调用者可以决定是否通过自己的回调直接回调或者将更多操作链接到承诺解决方案以及如何处理错误。
function getItemImages(items, paging, cb) {
var Promise = promise.Promise;
console.log("START",items,paging);
// Return a promise for items[i] with .imagedata
function promiseImage(i) {
return mongodbService.getItemImage(items[i]._id).
then(function(image) {
items[i].imagedata = image;
return items[i];
});
}
var promises = [];
for (var i=paging.pageOffset; i<Math.min(paging.pageOffset+paging.pageLimit,items.length); i++) {
if (!items[i].hasOwnProperty("imagedata")) {
console.log("LOADING "+i+":",items[i]);
// Add a new promise to promises
promises.push(promiseImage(i));
}
}
// promises is an array of promises, so "all" will work.
// Your code uses mutation, so the result isn't needed here.
// Previously, items was being sent to cb.
// cb might also consume the actual list of mutated items
// below.
Promise.all(promises).
then(() => {
console.log("RESULT",paging, items);
cb(paging, items);
});
}