-
-
Save jchannon/9628060 to your computer and use it in GitHub Desktop.
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
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'); | |
}); |
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
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')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment