Rules Hub
Code Ruleset
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)
then(callback).catch(callback) can invoke the same callback again after its success invocation throws.
Objašnjenje (HR)
Lanac then(callback).catch(callback) može drugi put pozvati isti callback ako njegova uspješna grana baci pogrešku.
Good example
| 1 | return promise.then( |
| 2 | result => callback(null, result), |
| 3 | error => callback(error, null), |
| 4 | ); |
Explanation (EN)
Only the original operation selects success or failure; the returned promise exposes any callback exception to the adapter's caller.
Objašnjenje (HR)
Samo izvorna operacija bira uspjeh ili neuspjeh; vraćeni promise izlaže pogrešku callbacka pozivatelju adaptera.
Notes (EN)
At a promise-to-callback boundary, keep operation rejection separate from exceptions thrown by the consumer callback. Return or observe the promise produced by then.
Bilješke (HR)
Na granici promisea i callbacka odvoji neuspjeh operacije od pogreške koju baci potrošačev callback. Vrati ili obradi promise koji proizvede then.
Exceptions / Tradeoffs (EN)
If parsing/transformation can throw, do it upstream of the callback branch so the callback receives that operation error. Two-argument then still returns a rejected promise if a callback throws; it does not consume that failure.