Skip to content

Instantly share code, notes, and snippets.

@adamloving
Last active September 24, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/68c101ecff39fb2fb523 to your computer and use it in GitHub Desktop.
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?
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