Skip to content

Instantly share code, notes, and snippets.

@bradparker
Last active November 17, 2016 05:51
Show Gist options
  • Select an option

  • Save bradparker/f886abdf6420d07fac2bc0ed94d725b8 to your computer and use it in GitHub Desktop.

Select an option

Save bradparker/f886abdf6420d07fac2bc0ed94d725b8 to your computer and use it in GitHub Desktop.
Promise pipeline with context object...
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