pub struct Then<A, B> { /* private fields */ }Expand description
Passes a service’s complete call result to a second service.
Created by crate::ServiceChain::then. The second service receives
Result<A::Res, A::Error> and can transform a successful response, recover
from an error, or return an error of its own. Its result becomes the final
result. Both services use the same state and error types.
Unlike crate::dev::AndThen, this invokes the second service even when
the first call fails.
§Readiness and lifecycle
Binding is forwarded to both children. Readiness succeeds only when both children are ready. Errors from the chain’s readiness check are returned directly, not passed to the second service as a request. Shutdown drives both children’s shutdown futures to completion.
Raw children share the enclosing readiness scope, and calls forward
directly without an extra readiness check. Wrap a child with
crate::ready() when it needs an independent cache and a call-time check,
particularly when the second child’s readiness can change while the first
call is awaiting.
Cloning clones both children; shared state and readiness depend on their clone implementations.
§Example
use ntex_service::{Service, fn_service, service};
let pipeline = service(fn_service(async |req: u32| {
if req == 0 { Err("missing value") } else { Ok(req) }
}))
.then(fn_service(async |result: Result<u32, &'static str>| {
Ok::<_, &'static str>(result.unwrap_or(10) * 2)
}))
.pipeline(());
assert_eq!(pipeline.call(3).await?, 6);
assert_eq!(pipeline.call(0).await?, 20);