Skip to content

Provide monadic syntax  #55

Open
Open
@dmtrKovalenko

Description

@dmtrKovalenko

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 ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions