this the first time I had to encounter a situation like this where a try catch block wasn't catching errors throwed inside onSnapshot
, so I had to use a nested tryCatch block inside it did what I was expecting but it kind looks dirty . I was wondering if this is avoidable ,my code :
const fetchTodaysOrders =async (arg,state)=>{
try {
const fetchOrdersReponse = await firestore()
.collection('orders')
.where('distrubutorId','==',currentDistrubutorId)
.where('status','==','PENDING')
fetchOrdersReponse.onSnapshot(res=>{
const docs= res.docs
try {
if(docs && docs.length){
//some code ...
}else{
throw new Error('NO_DOCS')
}
} catch (error) {
console.log("----fetchTodaysSectors catch2------")
dispatch.scheduel.fetchTodaysSectorsFailed()
}
})
} catch (error) {
//this wasn't hit when the error was encountered
console.log("----fetchTodaysSectors catch1------")
dispatch.scheduel.fetchTodaysSectorsFailed()
}
}
question from:
https://stackoverflow.com/questions/66068541/how-can-i-avoid-using-nested-try-catch-when-working-with-onsnapshot 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…