GPT4 说“错误的处理”应该是秒出结果的,它也没搞明白
// 需要订购的产品链接
const productList = [
"https://xxx",
"https://xxx",
"https://xxx"
]
// 是否有货
const hasStock = async (url: string): Promise[b] => {
const resp = await mAxios.get(url)
return resp.data.trim() !== ""
}
// 开始订购
const startOrder = async () => {
const promises = productList.map(u => ({tag: u, promise: hasStock(u)}))
// 根据是否有货判断购买
// 正确的处理
const results = await Promise.allSettled(promises.map(p => p.promise))
// 错误的处理
// const results = await Promise.allSettled(promises)
for (const [i, result] of results.entries()) {
// ...
// 是否有货,有就订购
if (!result.value) {
console.log("无货", promises[i].tag)
continue
}
}