catching
Catch promise reject, return as promise resolve.
Often used in async function, together with await.
Install
npm install catching
Use
async { const ret = await // Here you need differ resolve and reject by: code or instanceof Error, etc. const isOK = ... if !isOK throw ret else // ... next step }
Why
Normally when we use async/await, we would use try-catch for promise reject scenario.
But the code written by try-catch is not concise, while it indeed differentiates abnormal case from normal.
Code looks like below, addUserDAO
reject promise while meeting error.
async { try const ret = await catch err throw err } { return { // const isOK = ... if OK else }}
How
async { const ret = await if !retcode throw ret else // ... next step }