我有一个从一批fetch()
调用返回的Promise数组:
const fetchResults = [Promise, Promise, Promise]
每个Promise
的.body
都是ReadableStream,将其转换为string似乎是一个烦人的冗长的异步操作。我尝试使用await来读取正文字符串并将其映射回数组:
profileHTMLDocs.map(async (doc) => {
doc.text()
.then(async (html) => { return await html; })
})
我尝试了一些变体,但它似乎仍然返回一个promises数组,而不是html字符串数组。我遗漏了什么?是因为隐式返回是doc.text()
吗?
发布于 2018-06-03 03:27:06
您可能希望使用Promise.all将promise数组转换为解析为字符串数组的promise:
const result = Promise.all(fetchResults.map(p => p.then(res => res.text())));
https://stackoverflow.com/questions/50660377
复制相似问题