[英]Handle Promise not resolving when 404 occurs
I'm new with Promise.我是 Promise 的新手。
I have a vuejs component which fetch() function is like:我有一个 fetch() function 的 vuejs 组件,就像:
async fetch () {
this.loadingDetails = true
const products = await Promise.all(this.items.map((item) => {
try {
const products = this.$wizaplace.get('/catalog/products/' + item.declinationId.slice(0, 4), { // When the ID is incorrect, a 404 is returned
lang: this.$i18n.locale
})
console.log(products)
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error Fetching profile: ', e)
}
return products
}))
console.log(products)
this.productsDetails = products.map((product) => {
console.log(product)
const generalAttributes = product.attributes.find(attr => attr.id === 49)
const brewerie = generalAttributes.children.find(attr => attr.id === 59)
const color = generalAttributes.children.find(attr => attr.id === 15)
// console.log(product)
return {
attributes: product.attributes,
brewerie: (brewerie && brewerie.value[0]) ? brewerie.value[0] : '',
type: generalAttributes.children.find(attr => attr.id === 47).value[0],
color: (color && color.value[0]) ? color.value[0] : ''
}
})
this.loadingDetails = false
// console.log(this.productsDetails[0])
}, },
When one of the HTTP calls in the Promise.All(callback) fails, the Promise doesn't resolve.当 Promise.All(callback) 中的 HTTP 调用之一失败时,Promise 无法解析。 How could I handle this please?请问我该如何处理?
Thanks for any help !谢谢你的帮助 !
You are returning products outside the try block您在 try 块之外退回产品
You have to change that你必须改变它
const products = await Promise.all(this.items.map((item) => {
try {
const products = ...
// ...
} catch() {
// ...
}
return products
}))
with that接着就,随即
const products = await Promise.all(this.items.map((item) => {
try {
const products = ...
// ...
return products
} catch() {
// ...
}
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.