Promise.all() 方法未解析为值
Promise.all() method doesn't resolve to a value
以下代码在最后一个 then 处理程序中解析为 Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
。
尽管在将带有 promises 的数组记录到控制台时,每个都有状态 resolved
并且所有请求都已完成。
MusicService
.getArtists()
.then((res) => {
let arr = res.filter((a) => {
return a.id.length;
});
return Promise.all(arr.map(function(a) {
return fetch(`//service.com/api/artist/${a.id}`);
}));
})
.then((res) => {
console.log(res);
});
那么我缺少什么才能使它正常工作?
FWIW,这可以简化为:
MusicService
.getArtists()
.then(res => Promise.all(
res
.filter(a => a.id.length > 0)
.map(a => fetch(`//service.com/api/artist/${a.id}`))
)
)
.then(res => {
console.log(res);
});
在最后一个回调中,res
将是一个包含 fetch()
个结果的数组,不能是任何其他内容。
以下代码在最后一个 then 处理程序中解析为 Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
。
尽管在将带有 promises 的数组记录到控制台时,每个都有状态 resolved
并且所有请求都已完成。
MusicService
.getArtists()
.then((res) => {
let arr = res.filter((a) => {
return a.id.length;
});
return Promise.all(arr.map(function(a) {
return fetch(`//service.com/api/artist/${a.id}`);
}));
})
.then((res) => {
console.log(res);
});
那么我缺少什么才能使它正常工作?
FWIW,这可以简化为:
MusicService
.getArtists()
.then(res => Promise.all(
res
.filter(a => a.id.length > 0)
.map(a => fetch(`//service.com/api/artist/${a.id}`))
)
)
.then(res => {
console.log(res);
});
在最后一个回调中,res
将是一个包含 fetch()
个结果的数组,不能是任何其他内容。