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

Provide monadic syntax #55

Open
dmtrKovalenko opened this issue Jul 15, 2020 · 0 comments
Open

Provide monadic syntax #55

dmtrKovalenko opened this issue Jul 15, 2020 · 0 comments

Comments

@dmtrKovalenko
Copy link

It will be super helpful to have a monad-like syntax for Future. Here is something we are using for promises

/** Resolve promise, requires to manually handle promise */
let (>>-) = (m: Js.Promise.t('a), f: 'a => Js.Promise.t('b)) =>
  Js.Promise.then_(f, m);

/** Resolve promise with auto return */
let (>>=) = (m: Js.Promise.t('a), f: 'a => 'b) =>
  Js.Promise.then_(a => resolve(f(a)), m);

let (>>|) = (m: Js.Promise.t('a), f: 'a => 'b) =>
  Js.Promise.then_(
    a => {
      f(a);
      resolve(ignore);
    },
    m,
  );

/** Catch promise */
let (>>/) = (m: Js.Promise.t('a), f: Js.Promise.error => Js.Promise.t('a)) =>
  Js.Promise.catch(f, m);

/** Catch promise with auto return */
let (>>/=) = (m: Js.Promise.t('a), f: Js.Promise.error => 'a) =>
  Js.Promise.catch(e => Js.Promise.resolve(f(e)), m);

P.S. I could submit a PR for this if you don't mind. Thanks for the awesome work ;)

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

1 participant