These are chat archives for ramda/ramda

29th
Oct 2017
Nouman Saleem
@NoumanSaleem
Oct 29 2017 21:07

Hey All. Venturing into FP for the first time. Looking for help on using either with two promise operations to do a simple getOrCreate operation.

const getUser = ({ email }) => db.get(email);
const createUser = ({ email, name }) => db.create(email, name);

const getOrCreate = R.either(createUser, getUser);
getOrCreate({ email, name });

The problem I'm having is getUser returns undefined instead of false. Is there a recommended practice on handling this? or should I simply attach a .then to my getUser and have it return false when not getting data back

Nouman Saleem
@NoumanSaleem
Oct 29 2017 21:13
on second thought, I don't think either is something I can use with async operations. Thanks!
Michael Rosata
@mrosata
Oct 29 2017 21:15
@NoumanSaleem your correct, you don't want to use a function returning a promise inside either, because even if the promise is rejected, either will still return it.
you want to wait until the promises resolve/reject, then you'll have enough information to use either, or just then and catch
Nouman Saleem
@NoumanSaleem
Oct 29 2017 21:20
thanks @mrosata !
Michael Rosata
@mrosata
Oct 29 2017 21:25
@NoumanSaleem no problem, I wish I had a more functional answer for you :)
Possibly something like this, there's no Ramda. Basically have createUser take an email and name, but then have to be called again. So if getUser fails then createUser runs
const createUser = (email, name) => () => Promise.resolve({email, name})
const email = 'name@example.place'
const name = 'No Name Joe'
getUser(email)
  .catch(createUser(email, name))
  .then(user => {
    // here we have user
  })
Kurt Milam
@kurtmilam
Oct 29 2017 21:28
@NoumanSaleem also, there's been recent discussion about potentially adding an R.then function to help with Promise interop, and there are composeP and pipeP functions in Ramda, already, although there has been discussion of deprecating those in favor of R.then.
Michael Rosata
@mrosata
Oct 29 2017 21:29
that could work for sure
maybe const then = R.then || fn => promise => promise.then(fn) for now?
I didn't test that btw. Could set the world on fire
Kurt Milam
@kurtmilam
Oct 29 2017 21:39
Here's the R.then discussion.
I can never find it using GitHub search, for some reason. Luckily, I archived it in gmail...
Aha, I can't find it because it's a PR and not an issue :S
Michael Rosata
@mrosata
Oct 29 2017 21:41
:worried:
Kurt Milam
@kurtmilam
Oct 29 2017 21:45
@mrosata the implementation in that PR is basically what you suggested with a bit of extra checking added into the mix, so hopefully no fires :D