Skip to content

Instantly share code, notes, and snippets.

@GriffinHeart
Last active August 29, 2015 14:06
Show Gist options
  • Save GriffinHeart/485c2c677dff7728f791 to your computer and use it in GitHub Desktop.
Save GriffinHeart/485c2c677dff7728f791 to your computer and use it in GitHub Desktop.
waterfall to promises
Promise = require 'bluebird'
methodB = ->
p = new Promise (resolve, reject)->
setTimeout ->
console.log 'doing methodB'
reject 'failed b'
, 1000
p.catch (e)->
console.log 'log error to methodB.log'
p
methodC = ->
p = new Promise (resolve, reject)->
setTimeout ->
console.log 'methodC'
reject 'failed c'
, 1000
p.catch (e)->
console.log 'log error to methodC.log'
p
methodA = ->
console.log 'doing methodA'
methodB()
.catch (e)->
console.log "log that A needs B to execute"
Promise.reject e
.then ->
methodC().catch (e) ->
console.log "log that A needs C to execute"
methodA().catch (e)->
console.log 'error doing A warn client'
###
doing methodA
doing methodB
log error to methodB.log
log that A needs B to execute
error doing A warn client
###
// Generated by CoffeeScript 1.6.3
(function() {
var Promise, methodA, methodB, methodC;
Promise = require('bluebird');
methodB = function() {
var p;
p = new Promise(function(resolve, reject) {
return setTimeout(function() {
console.log('methodB');
return reject('failed b');
}, 1000);
});
p["catch"](function(e) {
console.log('log error to methodB.log');
return e;
});
return p;
};
methodC = function() {
var p;
p = new Promise(function(resolve, reject) {
return setTimeout(function() {
console.log('methodC');
return reject('failed c');
}, 1000);
});
p["catch"](function(e) {
console.log('log error to methodC.log');
return e;
});
return p;
};
methodA = function() {
console.log('doing methodA');
return methodB()["catch"](function(e) {
console.log('something failed on b');
return Promise.reject(e);
}).then(function() {
console.log('going for c');
return methodC()["catch"](function(e) {
console.log("something failed c");
return Promise.reject(e);
});
});
};
methodA();
/*
methodB
methodB failed handler
hoge
*/
}).call(this);
methodA (callback)->
async.waterfall [
(next)->
methodB (e)->
if e
# methodB failed handler
next e
do next
(next)->
methodC (e)->
if e
# methodC failed handler
next e
do next
], (e)->
return callback e if e
do callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment