Skip to content
New issue

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

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

Already on GitHub? # to your account

Async / Await #15

Open
mrispoli24 opened this issue Feb 17, 2020 · 3 comments
Open

Async / Await #15

mrispoli24 opened this issue Feb 17, 2020 · 3 comments

Comments

@mrispoli24
Copy link

I've read there's not async / await in reason. What's the best way to handle something like fetching from an API like a headless CMS? Are js promises the best way to handle this or is there a better way? How about features like generators / async generators?

@baransu
Copy link

baransu commented Feb 18, 2020

Monadic bind operator was recently introduced into OCaml but afaik there is no Reason support for that yet. In the meanwhile, you can use community reasonml-labs/bs-let which allows you to use that not only for Promises but also for other "monadic" data types like Option or Result.

module Option = {
  let let_ = Belt.Option.flatMap;
};

type address = {street: option(string)};

type personalInfo = {address: option(address)};

type user = {info: option(personalInfo)};

// Get the user's street name from a bunch of nested options. If anything is
// None, return None.
let getStreet = (maybeUser: option(user)): option(string) => {
  let%Option user = maybeUser;
  // Notice that info isn't an option anymore once we use let%Option!
  let%Option info = user.info;
  let%Option address = info.address;
  let%Option street = address.street;
  Some(street->Js.String.toUpperCase);
};

@fhammerschmidt
Copy link

While you can get pretty far with Js.Promise, the API is very unpleasant both from a JS developer and a functional developer perspective. The problem is that JavaScripts Promise specification is not monadic which makes it hard to write an elegant API for.

But imho @aantron recently did a pretty good job with the promise library and I think when you tie it with pipes, you can get a long way without async/await (or rather monadic let).

That being said, the PR including monadic let support just got merged, so it will be very soon possible to use an async/await-ish syntax in Reason.

@aantron
Copy link

aantron commented Feb 20, 2020

Thanks for the ping :) Coincidentally, I just opened aantron/promise#52 about async/await using the new let in Promise about this, at prompting of @mrmurphy.

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

No branches or pull requests

4 participants