-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Support let.await once Reason is updated in BuckleScript #52
Comments
|
How does the () => {
let.async x = foo();
let.async y = bar();
let.asyncMap z = baz();
x + y + z;
} |
Yes, there will have to be a separate suffix for Just thinking now, another option, which would clearly associate the |
I'm not sure that |
In my experience, in practice, you almost never end up using
rather than what we typically see written manually without
Because of the types, there is less opportunity to insert a |
Can you show an example of what this would look like using the syntax extension:
|
@jordwalke I added an experimental promise/src/native/promise.rei Lines 252 to 260 in c85cef9
Lines 586 to 589 in c85cef9
It's in Promise native only for now, because I didn't want to mess around with getting a development Reason into BuckleScript. I added some tests for the syntax, you can run them with
Unlike BTW don't worry about the underscore in |
To summarize the above comment, people won't be inserting |
I've been fiddling around a bit with using let-operators. I find it pretty difficult thinking about what could be defined as future conventions and best practices, but thought I'd share some ideas! Hope you're OK with some bikeshedding! 😅 Map the whole API to let-operators: let.flatMap
let.map
// ... etc
let.flatMapOk {Response.body, _} = fetch(”...”); Provide flatMap and map (with result-types), pro's are probably mostly for new onboarding: /* flatMap */
let.await
/* or, to follow OCaml-conventions */
let*await
/* map */
let+await
/* flatMapOk */
let.await?
/* mapOk */
let+await? |
@lessp have you written code that substantially uses |
Ok, so as an experiment, and trying to take on the role as a guinea pig, I used the /* retrieve all ID's of the top stories */
let.flatMapOk {Response.body} = fetch(".../v0/topstories.json");
/* parse the JSON into a list of postIds */
let postIds = /* ... implementation */
/* retrieve a post for each postId */
let.flatMap rawPosts =
posts
|> List.map(postId => fetch(".../v0/item/" ++ postId ++ ".json"))
|> Promise.all;
/* parse the JSON into a list of posts */
let posts = /* ... implementation */
Promise.resolved(Ok(posts)); /* Ok(posts) - if we'd use let.map above */ I'm not sure if this is helpful, but for me, who's main-reference of Don't know if that helps! Hackernews Endpointslet base = "https://hacker-news.firebaseio.com/v0"; |
Drive by comment below :D As a programmer who comes from PHP, JavaScript, some Rust to ReasonML, I'd much prefer sticking to the
|
The conclusion I arrived at, and the problem as I see it using We're already using let.flatMap user = Some(user);
let.flatMap user = Ok(user);
let.flatMap user = IO.retrieveUser(~id=1); I'm not very familiar with Rust, but IIUC you do not have to worry about mapping in its implementation of |
Not entirely sure if I'm interpreting your question correctly. As far as I'm aware Rust doesn't do anything special to await a promise (known as Future in Rust) returned from a function you're awaiting. Rust's implementation of async/await compiles away to a state machine. So you'd be left with a certain value in a certain state, that you'd have to I think my main problem with Rust's definition for
Even though a promise wraps a value, I don't see it as an iterator. The definition of
|
I think one can alias My main point is |
Sorry, what I meant was that, if Reason decided to go with something like: let foo: unit => Promise.t(User.t);
let async foo = () => {
let.await result = IO.retrieveUser(~id=1);
result;
}; instead of: let foo: unit => Promise.t(User.t);
let foo = () => {
let.flatMap result = IO.retrieveUser(~id=1);
Promise.resolve(result);
/* or `result`, if the final operation would be map instead of flatMap */
}; Then, IMHO, it might make more sense to use ...and as I understand it, in Rust, you'd do it similarly to that first example where you do not have to map over the result, but in Reason, you would. Hopefully someone can and will correct me if I'm wrong here. 🙂 |
@lessp Rust handles async/await as a separate language feature (similar to js). In Reason it's just another monad. This allows for using different implementations for async tasks. As long as they conform to being a monad the syntax will work with them. So one could use |
See reasonml/reason#2487.
The text was updated successfully, but these errors were encountered: