Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active February 16, 2021 05:12
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joepie91/34742045a40f7c48430e to your computer and use it in GitHub Desktop.
Save joepie91/34742045a40f7c48430e to your computer and use it in GitHub Desktop.
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