gpt4 book ai didi

JavaScript fetch() - 将 ReadableStreams 的数组映射到数组

转载 作者:行者123 更新时间:2023-11-29 20:53:16 30 4
gpt4 key购买 nike

我有一组从 fetch() 调用中返回的 Promise:

const fetchResults = [ promise , promise , promise ]

每个 Promise.bodyReadableStream , 将 which 转换为字符串似乎是一个烦人的冗长异步操作。我正在尝试使用 await 读取正文字符串并将其映射回数组:

profileHTMLDocs.map(async (doc) => { 
doc.text()
.then(async (html) => { return await html; })
})

我已经尝试了一些变体,但它似乎仍然返回一个 promises 数组而不是 html 字符串数组。我错过了什么?是因为隐式返回是doc.text()吗?

最佳答案

您可能想使用 Promise.all 将 promise 数组转换为解析为字符串数组的 promise:

const result = Promise.all(fetchResults.map(p => p.then(res => res.text())));

关于JavaScript fetch() - 将 ReadableStreams 的数组映射到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660377/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com