Skip to content

Instantly share code, notes, and snippets.

@phillipj
Created December 8, 2014 08:00
Show Gist options
  • Save phillipj/9aa46af4ef0adc03017a to your computer and use it in GitHub Desktop.
Save phillipj/9aa46af4ef0adc03017a to your computer and use it in GitHub Desktop.
Stream -> promise -> stream
var lifestyle = require('lifestyle');
var through2 = require('through2');
var client = new lifestyle.FinnClient('http://api.finn.no/iad/');
var mapToAd = through2.obj(function (adId, encoding, callback) {
client.getAd(adId).then(function(ad) {
this.push(ad);
}.bind(this), function(err) {
console.error('YIKES, GOT ERROR!', err);
}).finally(callback);
});
var pluckTitle = through2.obj(function (ad, encoding, callback) {
this.push(ad.title);
callback();
})
console.log('Enter any adId...');
process.stdin
.pipe(mapToAd)
.pipe(pluckTitle)
.pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment