Skip to main content

ps_promise/methods/
catch.rs

1use std::future::Future;
2
3use crate::{Promise, PromiseRejection};
4
5impl<T, E> Promise<T, E>
6where
7    T: Send + Unpin + 'static,
8    E: PromiseRejection,
9{
10    pub fn catch<TO, EO, F, Fut>(self, recover: F) -> Promise<TO, EO>
11    where
12        TO: From<T> + Unpin + 'static,
13        EO: PromiseRejection + 'static,
14        F: FnOnce(E) -> Fut + Send + 'static,
15        Fut: Future<Output = Result<TO, EO>> + Send + 'static,
16    {
17        Promise::new(async move {
18            match self.await {
19                Ok(value) => Ok(value.into()),
20                Err(err) => recover(err).await,
21            }
22        })
23    }
24}