data look like this
async getName() {
let res = await fetch(xxx)
res = await res.json()}
return res;}
async getAddr(){
let res = await fetch(xxx)
res = await res.json()}
return res
}
In vuejs component
async mounted (){
this.showLoader=true
let res = await Promise.all([getName(),getAddr()]
//remove loader if response array has been resolved successfully
this.showLoader=false
The problem is here, how do I add a timeout handler for promise.all()?
If over 10 seconds then I would remove the loader component and pop out failed connection notifications ?
Where the handler should be added?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race Maybe this is something you can use?
if the timeout is by solid design, promise.race() as suggested by @molteber would work. but if you need to catch failed network connection, use try catch block.
Обсуждают сегодня