Hi.
I've managed to get the following code compiling:
pub fn post_register(register_data: Json<Register>, state: State<api::State>) -> FutureResponse<HttpResponse> {
tokio_timer::sleep(Duration::from_millis(400)).into_future()
.map_err(|_| api::Error::Internal)
// .map_err(|_| actix::prelude::MailboxError::Timeout)
// .from_err()
// .map(|x| futures::future::ok(x))
// .flatten()
.and_then(move |_|
state
.db
.send(GetAllPendingAccounts).flatten()
)
.map_err(|_| api::Error::Internal)
// .from_err()
// .flatten()
.and_then(move |res| {
let res = api::Response::data(res);
Ok(HttpResponse::Ok().json(&res))
})
.map_err(|_| api::Error::Internal)
.from_err()
.responder()
// Ok(HttpResponse::Ok().json(&res))
// Ok(api::Response::data(register_data.0))
}
But I'm not sure what is happening step by step.
What I'm trying to do is:
- wait for 400ms without blocking reactor, if an error happens returns an
api::Error::Internal
- then send a
GetAllPendingAccounts
to my db Actor - then send the data
- if any of these steps fails, send
api::Error::Internal
.
What I'm not sure to understand is:
- if one of the futures fails, when does the chain break ?
- Why I need to append
flatten
after.send(GetAllPendingAccounts)
to get it working (otherwise I have Error conversion errors) ? - why I need to add
.map_err(|_| api::Error::Internal)
after eachand_then
step ? - in a chain of futures, all 'steps' must returns the same error type right ?
- What is the best way to
and_then
a future withoutflaten
?