-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
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);
}; |
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 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. |
Thanks for the ping :) Coincidentally, I just opened aantron/promise#52 about async/await using the new |
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?
The text was updated successfully, but these errors were encountered: