Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prabirshrestha/9627836 to your computer and use it in GitHub Desktop.
Save prabirshrestha/9627836 to your computer and use it in GitHub Desktop.
promise on array and streams. http://stackoverflow.com/q/22475917/157260
var fs = require('fs'),
path = require('path'),
Promise = require('rsvp').Promise,
request = require('request'),
files = [
'https://www.google.com/images/srpr/logo11w.png',
'http://rack.1.mshcdn.com/media/ZgkyMDEzLzA5LzE2L2JjL0Jpbmdsb2dvb3JhLmFkYjJkLnBuZwpwCXRodW1iCTEyMDB4OTYwMD4/996d9598/35b/Bing-logo-orange-RGB.png',
'http://assets.fontsinuse.com/static/use-media-items/15/14246/full-2048x768/52c4c6bc/Yahoo_Logo.png'
];
var process = files.map(function (url) {
return downloadFile(url).then(extractFile);
});
Promise.all(process)
.then(function () {
console.log('done processing');
})
.then(null, function () {
console.log('error processing');
});
var fs = require('fs'),
path = require('path'),
Promise = require('rsvp').Promise,
request = require('request'),
files = [
'https://www.google.com/images/srpr/logo11w.png',
'http://rack.1.mshcdn.com/media/ZgkyMDEzLzA5LzE2L2JjL0Jpbmdsb2dvb3JhLmFkYjJkLnBuZwpwCXRodW1iCTEyMDB4OTYwMD4/996d9598/35b/Bing-logo-orange-RGB.png',
'http://assets.fontsinuse.com/static/use-media-items/15/14246/full-2048x768/52c4c6bc/Yahoo_Logo.png'
];
// waterfall - process next file only after previous one is done
files.reduce(function (sequence, next) {
return sequence.then(function () {
return downloadFile(next).then(extractFile);
});
}, Promise.resolve())
.then(function () {
console.log('done processing files');
})
.then(null, function (err) {
console.log('error processing files', err);
});
function downloadFile(url) {
return new Promise(function (resolve, reject) {
var destination = path.basename(url);
request(url).pipe(fs.createWriteStream(destination))
.on('close', function () {
resolve(url);
})
.on('error', function (err) {
reject(err);
});
});
}
function extractFile (url) {
var file = path.basename(url);
if (path.extname(file) !== '.zip') {
return Promise.resolve(url); // NOOP
}
// extract file
return Promise.reject(new Error('not implemented'));
}
var fs = require('fs'),
path = require('path'),
request = require('request'),
Rx = require('rx'),
files = [
'https://www.google.com/images/srpr/logo11w.png',
'http://rack.1.mshcdn.com/media/ZgkyMDEzLzA5LzE2L2JjL0Jpbmdsb2dvb3JhLmFkYjJkLnBuZwpwCXRodW1iCTEyMDB4OTYwMD4/996d9598/35b/Bing-logo-orange-RGB.png',
'http://assets.fontsinuse.com/static/use-media-items/15/14246/full-2048x768/52c4c6bc/Yahoo_Logo.png'
];
Rx.Observable.fromArray(files)
.selectMany(function downloadFile(url) {
var subject = new Rx.AsyncSubject(),
destination = path.basename(url);
request(url).pipe(fs.createWriteStream(destination))
.on('close', function () {
subject.onNext(url);
subject.onCompleted();
})
.on('error', function (err) {
subject.onError(err);
});
return subject;
})
.filter(function (file) {
return path.extname(file) === '.zip';
})
.selectMany(function unzip (file) {
return Rx.Observable.throw(new Error('not implemented'));
})
.subscribe(function onNext () {
}, function onError (err) {
console.log(err);
}, function onCompleted () {
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment