For now, I am fixing the code to use some functions that don’t cause exceptions.
Before code was consisting of function compositions with Lwt.Infix.(>>=).
But now also some variables in the code return (string, [ ``Msg of string ]) Stdlib.result instead of string.
To avoid using pattern matching here, I want to use result binding too. Is there any way to do that?
I am asking about best idiomatic OCaml ways to do this, not just every single way possible.
Because the pattern of having a result inside a promise is so common (e.g. an HTTP request that might fail), Async provides a “Deferred.Result” monad where the type is a Result.t Deferred.t, with the associated infix operators and let-syntax. And if it’s awkward to open that module, then there are also infix bind and map operators >>=? and >>|? exposed in Async itself. So one option would be to implement the equivalent of those functions for Lwt, assuming they don’t exist already.
I think this is the best solution I saw. With some internal beauty.