Last active
August 29, 2015 14:06
-
-
Save pchw/0a31709103144663f081 to your computer and use it in GitHub Desktop.
Promise Sample
This file contains hidden or 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
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 | |
### |
This file contains hidden or 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
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 | |
### |
This file contains hidden or 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
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