Functional programming (map, filter, reduce) in bluebird
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
/* 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