2016-11-18 49 views
1

我希望能夠從承諾的功能之外捕捉到承諾內拋出的異常。請看下面的代碼:如何捕捉內部異常?

throwError =() => { 
    throw new Error("myError"); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return (new Promise((resolve) => { 
     resolve() 

    }).then(() => { 
     (new Promise((resolve, reject) => { 
      return throwError(); 
     })); 
     return new Promise((resolve) => setTimeout(resolve, 1000)); 
    })).catch((error) => { 
     //Never reaches here 
     console.log(error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //Never reaches here 
    console.log(error); 
}); 

我能趕上內部錯誤,但我不能把它結束了,我想(到底),通過在中間增加一個問題:

let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return (new Promise((resolve) => { 
     resolve() 

    }).then(() => { 
     (new Promise((resolve, reject) => { 
      return throwError(); 
     })).catch((error) => { 
      //Prints, but thrown error isn't bubbled anywhere 
      console.log(error); 
      throw new Error(error); 
     }); 
     return new Promise((resolve) => setTimeout(resolve, 1000)); 
    })).catch((error) => { 
     //Never reaches here 
     console.log(error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //Never reaches here 
    console.log(error); 
}); 

所有測試中7.1.0的NodeJS是運行

+0

在內部回調中,您會進行各種投擲和重新投擲,然後返回一個無關的,已解決的承諾。毫不奇怪,外層的'.catch()'沒有什麼可抓的。 –

回答

0

我意識到我的問題是不合理的,因爲我想拒絕已經解決的承諾。問題是我想立即從承諾中繼續,而不是等待它解決/拋出。一個更清晰的例子會是這樣的:

writeToDatabase =() => { 
    return new Promise((resolve, reject) => { 
     setTimeout(() => { 
      if (hasPermissions) { 
       resolve() 
      } else { 
       reject(); 
      } 
     }, 1000) 
    }); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
     resolve() 
    }).then(() => { 
     //Don't want to wait for this to complete 
     //but want to catch the error in later catch clauses 
     //Unfortunately, this is unreasonable since I can't reject 
     //a promise that might already have been resolved. 
     //Therefore, refactor to have the same method to handle error 
     writeToDatabase().catch(handleError); 

     return Promise.resolve(); 
    }).catch((error) => { 
     //Will not catch the database error, since it's already resolved 
     console.log('err', error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    // Will not catch the database error, since it's already resolved 
    // Instead, we can reuse the same error handling function 
    handleError(error); 
}); 
1

編輯您的代碼如下: -

throwError =() => { 
throw new Error("myError"); 
}; 


let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
     resolve() 
    }).then(() => { 
     //since you are resolving promise , this will be called 
     return Promise.reject('err') 
     // returning rejection so we can catch later 
    }).catch((error) => { 
      //It will not be called unless some error is thrown 
      console.log('err',error) 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
    }).catch((error) => { 
    // Since a rejection is returned by function , it is called 
    console.log(error); 
}); 

或者,如果你想牛逼o拋出錯誤: -

throwError =() => { 
    throw new Error("myError"); 
}; 

let iWantToCatchTheErrorThatThisOneThrows =() => { 
    return new Promise((resolve) => { 
    resolve() 

    }).then(() => { 

    return throwError(); 

    }).catch((error) => { 
    //Reaches 
    console.log('err',error) 
    //Rejection returned to catch later 
    return Promise.reject('rejection from catch') 
    }); 
}; 

iWantToCatchTheErrorThatThisOneThrows().then(() => { 
    console.log("Quit gracefully"); 
}).catch((error) => { 
    //rejection caught 
    console.log(error); 
}); 
+0

它並不完全適用於我的用例,因爲這需要我等待拋出錯誤的承諾 – Karamell