使用異步方法來抓取錯誤是一種反模式嗎?
await something().catch(aysnc (err) => {
console.log(err)。
await rollback()。
});
我這樣做是因為我需要在讓用戶再次執行該方法之前確保回滾已經被執行:
const method = async()=> {
if(isExecuting.current) return;
isExecuting.current = true;
await something().catch(aysnc (err) => {
console.log(err)。
await rollback(); <--我也需要等待這個。
});
isExecuting.current = false;
uj5u.com熱心網友回復:
你可以用try和catch
const method = async() => {
if (isExecuting.current) return;
isExecuting.current = true;
try {
await something()。
} catch (err) {
console.log(err)。
await rollback()。
}
isExecuting.current = false;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/318052.html
標籤:
