Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reuse a former value in neverthrow pipeline? #611

Open
eakl opened this issue Nov 12, 2024 · 1 comment
Open

How to reuse a former value in neverthrow pipeline? #611

eakl opened this issue Nov 12, 2024 · 1 comment

Comments

@eakl
Copy link

eakl commented Nov 12, 2024

Hi, I'm new to neverthrow (and functional programming) and I can't understand how to reuse a former value in a pipeline

function run() {
   return fromPromise(
      getData(),
      () => new Error('error getting data')
   )
   .andThen((fetched) => ok(add(fetched, 5)))
   .andThen((added) => ok(multiply(added, 2)))
   .andThen((multiplied) => ok(substract(multiplied, 3)))
   .andThen((substracted, fetched) => ok(add(substracted, fetched))) // fetched is not accessible here
   .match(
      (value) => value,
      (error) => error
   )
}
@nikita-demin-snkeos
Copy link

unfortunately, there is no way to reuse a former value. you basically have 2 options here, either "pause" a pipeline mid-way, unwrap the value you need and then start a new one where later you can refer to it. alternatively you may introduce a more complex value in your result to carry the value you need, the approach is similar to the state monad in haskell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants