Skip to content

Instantly share code, notes, and snippets.

@pchw
Last active August 29, 2015 14:06
Show Gist options
  • Save pchw/4bed1d2b958ad029dda1 to your computer and use it in GitHub Desktop.
Save pchw/4bed1d2b958ad029dda1 to your computer and use it in GitHub Desktop.
promise reject flow
Promise = require 'bluebird'
methodC = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodC'
reject 'methodC failed'
, 1000
methodB = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodB'
reject 'methodB failed'
, 1000
methodA = (param)->
unless param
return Promise.reject 'methodA Reject'
Promise.resolve(1)
.then ->
methodB()
.catch (e)->
console.log 'methodB failed handler'
Promise.reject e
.then ->
methodC()
.catch (e)->
console.log 'methodC failed handler'
Promise.reject e
console.log 'start'
methodA('p')
.then (v)->
console.log "this is then. #{v}"
.catch (e)->
console.log "this is catch. #{e}"
console.log 'end'
###
start
end
methodB
methodB failed handler
methodC failed handler
this is catch. methodB failed
###
Promise = require 'bluebird'
methodC = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodC'
reject 'methodC failed'
, 1000
methodB = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodB'
reject 'methodB failed'
, 1000
methodA = (param)->
unless param
return Promise.reject 'methodA Reject'
call_methodB = ->
methodB()
.catch (e)->
console.log 'methodB failed handler'
Promise.reject e
call_methodC = ->
methodC()
.catch (e)->
console.log 'methodC failed handler'
Promise.reject e
Promise.resolve(1)
.then ->
call_methodB()
.then ->
call_methodC()
console.log 'start'
methodA('p')
.then (v)->
console.log "this is then. #{v}"
.catch (e)->
console.log "this is catch. #{e}"
console.log 'end'
###
start
end
methodB
methodB failed handler
this is catch. methodB failed
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment