Skip to content

add a try sync to make working with results and promises easier#24

Open
CrowdHailer wants to merge 1 commit into
gleam-lang:mainfrom
CrowdHailer:try_sync
Open

add a try sync to make working with results and promises easier#24
CrowdHailer wants to merge 1 commit into
gleam-lang:mainfrom
CrowdHailer:try_sync

Conversation

@CrowdHailer
Copy link
Copy Markdown
Contributor

I've ended up adding this to nearly every js project I create

The most recent use looks like this.

  use response <- promise.try_sync(return |> result.map_error(string.inspect))

  case response.status {
    200 -> {
      use reader <- promise.try_sync(
        fetch.bytes_reader(response) |> result.map_error(string.inspect),
      )
      do_stream(reader, provider, <<>>, chat.fresh())
    }
    status -> {
      use body <- promise.await(fetch.read_text_body(response))
      let reason = case body {
        Ok(response) -> "Failed to reach llm: " <> response.body
        Error(_) -> "Failed to call llm, status: " <> int.to_string(status)
      }
      promise.resolve(Error(reason))
    }
  }

@CrowdHailer
Copy link
Copy Markdown
Contributor Author

The tests are an exact copy of the try_await tests with results not promise of results

Copy link
Copy Markdown
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! When would one use this function? Could you explain the motivations for it please 🙏 Including examples and what the alternatives are. It's not immediately obvious when I would use this function.

@CrowdHailer
Copy link
Copy Markdown
Contributor Author

CrowdHailer commented Mar 18, 2026

To come up with a contrived example think of a process where you want to chain result processing where some functions return promise or results but others just return results. For example getting a header and reading the body from a response.

neglect for now differences in error type

This won't work

use header <- result.try(response.get_header(response, "x-header"))
use body <- promise.try_await(fetch.read_text_body(response))
... more function

Probably the simplest fix is

use header <- promise.try_await(promise.resolve(response.get_header(response, "x-header")))
use body <- promise.try_await(fetch.read_text_body(response))
... more function

But this creates a promise to immediately discard it in the call to try_await. and is noisy

@CrowdHailer CrowdHailer reopened this Mar 18, 2026
@halostatue
Copy link
Copy Markdown

This is almost identical to pontil.try_promise which I've needed for the same reason, because it's not really possible to use result.try when you have promise chains and you're returning Promise(Result(a, e)) because your Error results need to be wrapped in a promise as well.

Even if I were to modify pontil's reexports of pontil/core so that everything returns a promise, I'd need this as an internal function to make the responses promise-based.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants