Skip to content

Instantly share code, notes, and snippets.

@pchw
Last active August 29, 2015 14:06
Show Gist options
  • Save pchw/0a31709103144663f081 to your computer and use it in GitHub Desktop.
Save pchw/0a31709103144663f081 to your computer and use it in GitHub Desktop.
Promise Sample
Promise = require 'bluebird'
methodB = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodB'
reject 'hoge'
, 1000
methodA = (param)->
p = new Promise (resolve, reject)->
unless param
return reject 'methodA Reject'
else
return do resolve
p
.then ->
methodB()
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
this is catch. hoge
###
Promise = require 'bluebird'
methodB = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodB'
reject 'hoge'
, 1000
methodA = (param)->
new Promise (resolve, reject)->
unless param
return reject 'methodA Reject'
else
methodB()
.then ->
return do resolve
.catch (e)->
return reject 'methodA Catch'
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
this is catch. methodA Catch
###
Promise = require 'bluebird'
methodB = ->
new Promise (resolve, reject)->
setTimeout ->
console.log 'methodB'
reject 'hoge'
, 1000
methodA = (param)->
new Promise (resolve, reject)->
unless param
return reject 'methodA Reject'
else
resolve methodB()
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
this is catch. hoge
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment