13

What's the proper way to interrupt a long chain of compose or pipe functions ?

Let's say the chain doesn't need to run after the second function because it found an invalid value and it doesn't need to continue the next 5 functions as long as user submitted value is invalid.

Do you return an undefined / empty parameter, so the rest of the functions just check if there is no value returned, and in this case just keep passing the empty param ?

Robert Brax
  • 6,508
  • 12
  • 40
  • 69

2 Answers2

5

I don't think there is a generic way of dealing with that.

Often when working with algebraic data types, things are defined so that you can continue to run functions even when the data you would prefer is not present. This is one of the extremely useful features of types such as Maybe and Either for instance.

But most versions of compose or related functions don't give you an early escape mechanism. Ramda's certainly doesn't.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
4

While you can't technically exit a pipeline, you can use pipe() within a pipeline, so there's no reason why you couldn't do something like below to 'exit' (return from a pipeline or kick into another):

pipe(
  // ... your pipeline 
  propOr(undefined, 'foo'), // - where your value is missing
  ifElse(isNil, always(undefined), pipe(
    // ...the rest of your pipeline
  ))
)
MorganIsBatman
  • 1,000
  • 8
  • 13