Interested in ReScript until I read async/await docs

Are you replying to me?

I said having more complex examples we have here procured on the docs page or a cookbook is always helpful. Im unsure how far OP dove to find examples but doesn’t hurt to consider.

I’m also assessing Rescript right now and I came up with writing a little prelude where I wrap a Belt.Result in a Js.Promise in a dedicated data type for which I wrote a few helper functions : https://github.com/err0r500/realworld-app-backend-rescript-ts/blob/2f20dfe263463bce392610e90a4faa6b9ad4991e/libs/prelude.res#L50

There’s a very basic usage example here : https://github.com/err0r500/realworld-app-backend-rescript-ts/blob/2f20dfe263463bce392610e90a4faa6b9ad4991e/src/usecase/registration.res#L13 but I tried it on a much more complex logic flow and it worked smoothely.

1 Like

If you want to combine promises with results, you might be interested in aantron/promise, it works quite well!

2 Likes

Actually using result is a good thing. I encourage converting promise value to result as soon as possible and avoid using promise catch. There are a lot of good articles about separation of error and exeption flows.
But it’s better to not create a new result type for async operations, and use an existing one with some utility functions for convinient work with promise.
Personally I dont use any library for it. I only use Belt.Result inside of thenResolve and one extra function:

// ResultExtra.res
let asyncFlatMap = (
  self: result<'value, 'error>,
  fn: 'value => Promise.t<result<'newValue, 'error>>,
): Promise.t<result<'newValue, 'error>> => {
  switch self {
  | Error(_) as error => error->Promise.resolve
  | Ok(value) => fn(value)
  }
}