是否有办法从未来的合并者链中尽早返回?例如,如果某些条件成立,我想返回成功,否则继续沿链前进。
我想做类似的事情:
fn early_exit() -> impl Future<Item = u32, Error = String> {
ok::<u32, String>(42)
.and_then(some_function)
.and_then(another_function)
.and_then(|x| {
// return early if condition is satisfied
if some_condition(x) {
return ok::<u32, String>(42);
}
// Else pass it down the combinator chain for further transformations
else {
Ok(x)
}
})
.and_then(one_more_function)
.and_then(last_function)
}
我认为使用async
/ await
可以做到这一点,但是用期货0.1可以做到这一点吗?