Skip to content

Commit

Permalink
docs: add returning on the first error example for try_join! (tokio-r…
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-rustin authored and suikammd committed Oct 7, 2021
1 parent 3780c66 commit cb646b1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tokio/src/macros/try_join.rs
Expand Up @@ -59,6 +59,45 @@
/// }
/// }
/// ```
///
/// Using `try_join!` with spawned tasks.
///
/// ```
/// use tokio::task::JoinHandle;
///
/// async fn do_stuff_async() -> Result<(), &'static str> {
/// // async work
/// # Err("failed")
/// }
///
/// async fn more_async_work() -> Result<(), &'static str> {
/// // more here
/// # Ok(())
/// }
///
/// async fn flatten<T>(handle: JoinHandle<Result<T, &'static str>>) -> Result<T, &'static str> {
/// match handle.await {
/// Ok(Ok(result)) => Ok(result),
/// Ok(Err(err)) => Err(err),
/// Err(err) => Err("handling failed"),
/// }
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let handle1 = tokio::spawn(do_stuff_async());
/// let handle2 = tokio::spawn(more_async_work());
/// match tokio::try_join!(flatten(handle1), flatten(handle2)) {
/// Ok(val) => {
/// // do something with the values
/// }
/// Err(err) => {
/// println!("Failed with {}.", err);
/// # assert_eq!(err, "failed");
/// }
/// }
/// }
/// ```
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
macro_rules! try_join {
Expand Down

0 comments on commit cb646b1

Please sign in to comment.