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

F.when/unless different return type to input type? #30

Closed
Darkle opened this issue May 24, 2022 · 1 comment
Closed

F.when/unless different return type to input type? #30

Darkle opened this issue May 24, 2022 · 1 comment

Comments

@Darkle
Copy link

Darkle commented May 24, 2022

Hi, I have a use case where I would like to use F.when, but have the return type be different to the type going in to the function. At the moment the return type for F.when and F.unless can only be the input type.

The context here is creating a document in a mongodb database and first checking to see if the document already exists. If it exists (checked via MyModel.exists()), a document object is returned, if it doesnt exist, null is returned.

So I would like to do something like this:

MyModel.exists({foo:'bar'})
  .then(O.fromNullable)
  .then(F.when(O.isNone, () => MyModel.create({foo:'bar'})))
@mobily
Copy link
Owner

mobily commented Jun 5, 2022

@Darkle hm, in this specific case, I'd use a different approach:

MyModel.exists({foo:'bar'})
  .then(O.fromNullable)
  .then(O.map(() => MyModel.create({foo:'bar'}))
  .then(O.toUndefined)

// or

MyModel.exists({foo:'bar'})
  .then(
    flow(
      O.fromNullable,
      O.map(() => MyModel.create({foo:'bar'}),
      O.toUndefined,
    )
  )

in terms of F.when and F.unless usage, both TS signatures have been updated in v3.12.0, and now it's possible to return a different type

@mobily mobily closed this as completed Jun 5, 2022
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