Skip to content

Instantly share code, notes, and snippets.

@orzklv
Created January 8, 2026 20:57
Show Gist options
  • Select an option

  • Save orzklv/dc485d4921b59baba87b2cd163d10025 to your computer and use it in GitHub Desktop.

Select an option

Save orzklv/dc485d4921b59baba87b2cd163d10025 to your computer and use it in GitHub Desktop.
Async .and_then() for your perfectionism!
pub trait AsyncResult<T, E> {
async fn and_then_async<U, F: AsyncFnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E>;
}
impl<T, E> AsyncResult<T, E> for std::result::Result<T, E> {
#[inline]
async fn and_then_async<U, F: AsyncFnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
match self {
Ok(t) => op(t).await,
Err(e) => Err(e),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment