Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Created June 12, 2013 10:55
Show Gist options
  • Save jbgutierrez/5764369 to your computer and use it in GitHub Desktop.
Save jbgutierrez/5764369 to your computer and use it in GitHub Desktop.
Harmony Generators vs Promisses Libs
var step = 0;
async.waterfall([
function (next){
step = 1;
getJson('/country', function(data) { next(null, data); });
},
function (country, next){
step = 2;
getJson('/weather', function(data) { next(null, country, data); });
},
function (country, weather, next){
step = 3;
getJson('/stock_exchange', function(data) { next(null, country, weather, data); });
}
], function (country, weather, stockExchange) {
if (err) {
console.log("Died at step:" + step);
} else {
console.dir({
country: country,
weather: weather,
stockExchange: stockExchange
});
}
});
// vs:
// see: https://gist.github.com/creationix/5544019
run(function* () {
var country, weather, stockExchange, step = 1;
try {
step++;
country = yield getJson('/country');
step++;
weather = yield getJson('/weather');
step++;
stockExchange = yield getJson('/stock_exchange');
console.dir({
country: country,
weather: weather,
stockExchange: stockExchange
});
} catch(err) {
console.log("Died at step:" + step);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment