Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Functional programming (map, filter, reduce) in bluebird
/* Double all numbers */
Promise.map([1, 2, 3], function(num) {
return num * 2;
}).then(function(numbers) {
console.log("The final list of numbers:", numbers);
//The final list of numbers: [ 2, 4, 6 ]
});
/* Remove all the odd numbers */
Promise.filter([1, 2, 3], function(num) {
return (num % 2) == 0;
}).then(function(numbers) {
console.log("The final list of numbers:", numbers);
// The final list of numbers: [ 2 ]
});
/* Sum all the numbers */
Promise.reduce([1, 2, 3], function(total, num) {
return total + num;
}, 0).then(function(number) {
console.log("The final value:", number);
// The final value: 6
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.