Last active
September 24, 2015 18:08
-
-
Save adamloving/68c101ecff39fb2fb523 to your computer and use it in GitHub Desktop.
When using when.js, should you put the catch first or last in the promise chain?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w = require 'when' | |
# When using when, should you put the catch first or last in the promise chain? | |
# You should put it last if you want it to end the chain before your thens run. | |
doSomethingAndFail = (n) -> | |
console.log 'fail', n | |
w.reject(n) | |
doSomethingAndFail(1) | |
.then (n) -> | |
console.log 'then after fail', n # never runs | |
.catch (n) -> | |
console.log 'caught failure at end of chain', n | |
doSomethingAndFail(2) | |
.catch (n) -> | |
console.log 'caught failure at beginning of chain', n | |
.then (n) -> | |
console.log 'then after catch', n # runs with n == undefined | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment