Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use the two-argument then so a rejection handler cannot catch the success handler's own throw
Chaining then(cb).catch(cb) means anything the success callback throws is caught by the adjacent catch and the same callback runs a second time, this time with an error. Pass both handlers to then so they stay mutually exclusive.
Bad example
| 1 | promise |
| 2 | .then((result) => callback(null, result)) |
| 3 | .catch((error) => callback(error, null)); |
| 4 | // if callback throws inside .then, .catch calls it again with the error |
Explanation (EN)
A consumer written as if (err) reject(); resolve(res) can see both branches fire for one operation.
Objašnjenje (HR)
Potrosac napisan kao if (err) reject(); resolve(res) moze vidjeti kako se za jednu operaciju izvrse obje grane.
Good example
| 1 | promise.then( |
| 2 | (result) => callback(null, result), |
| 3 | (error) => callback(error, null), |
| 4 | ); |
Explanation (EN)
The rejection handler only observes the original promise, so exactly one branch runs per operation.
Objašnjenje (HR)
Rukovatelj odbijanjem promatra samo izvorni promise, pa se po operaciji izvrsava tocno jedna grana.
Notes (EN)
This is the difference between then(onFulfilled, onRejected) and then(onFulfilled).catch(onRejected). It only matters when the success handler can throw, which is exactly what a consumer callback does.
Bilješke (HR)
To je razlika izmedu then(onFulfilled, onRejected) i then(onFulfilled).catch(onRejected). Bitno je samo kad uspjesni rukovatelj moze baciti gresku, a upravo to radi korisnicki callback.