Skip to content

Instantly share code, notes, and snippets.

@chinmay185
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chinmay185/ecbdb8ed141511499323 to your computer and use it in GitHub Desktop.
Save chinmay185/ecbdb8ed141511499323 to your computer and use it in GitHub Desktop.
Demonstration of async map reduce using promises
var Promise = require("bluebird");
var fileParts = ["http://myfiles.com/file1/part1",
"http://myfiles.com/file1/part2",
"http://myfiles.com/file1/part3"];
var fetchPart = function(partUrl) {
// returns the promise of the part of a file
};
var filePartPromises = fileParts.map(fetchPart);
var file = [];
filePartPromises
.reduce(function(acc, part) {
return acc
.then(function() {
file.push(part);
})
.catch(console.log);
}, q.resolve())
.then(function() {
console.log("complete file ", file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment