Last active
November 17, 2016 05:51
-
-
Save bradparker/f886abdf6420d07fac2bc0ed94d725b8 to your computer and use it in GitHub Desktop.
Promise pipeline with context object...
This file contains hidden or 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
| const { assign } = Object | |
| const mergeResults = (pipeline, step) => ( | |
| pipeline.then((context) => ( | |
| step(context).then((result) => ( | |
| assign({}, context, result) | |
| )) | |
| )) | |
| ) | |
| const createPipeline = (...steps) => (context) => ( | |
| steps.reduce(mergeResults, Promise.resolve(context)) | |
| ) | |
| const pipeline = createPipeline( | |
| ({ start }) => Promise.resolve({ upper: start.toUpperCase() }), | |
| ({ upper }) => Promise.resolve({ reverse: upper.split('').reverse().join('') }) | |
| ) | |
| pipeline({ start: 'Hey there' }).then((result) => console.log(result)) | |
| // Logs: | |
| // { start: 'Hey there', upper: 'HEY THERE', reverse: 'EREHT YEH' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment