Last active
August 29, 2015 14:20
-
-
Save chinmay185/ecbdb8ed141511499323 to your computer and use it in GitHub Desktop.
Demonstration of async map reduce using promises
This file contains 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 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