- Lodash provides flow function which helps in sequencing multiple functions in series.
- Lodash _.flow() method takes the function names as parameters and executes in sequence by binding to current scope.
- In this demo, “We will learn how to use lodash flow functions”.
- The following code shows 4 methods substractByOne(), addByTwo(),multiplyByThree() and divideByFour(). Lodash _.flow() method takes all these 4 functions and executes in series.
<!DOCTYPE html> <html> <head> <script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script> <meta charset="utf-8"> <title>lodash flow function</title> </head> <body> <script> var substractByOne = function(number) { return number - 1; }; var addByTwo = function(number) { return number + 2; }; var multiplyByThree = function(number) { return number * 3; }; var divideByFour = function(number) { return number / 4; }; var flowFunctionResult = _.flow(substractByOne, addByTwo, multiplyByThree, divideByFour); var result = flowFunctionResult(6); console.log("Result: " + result); </script> </body> </html>
- The output of the previous code can be found in the following JSBIN link.