Handling errors in futures
In a real application, we would not be returning a value instantly from an asynchronous function that directly returns Async::Ready<T>
or FutureResult<T, E>
. Network requests time out, buffers become full, services become unavailable due to bugs or outages, and many more issues pop up on a daily basis. As much as we like to build order from chaos, usually chaos wins due to naturally-occurring entropy (programmers may know this as scope creep) and decay (software updates, new computer science paradigms, and so on). Luckily for us, the futures library offers us a simple way to implement error handling.
How to do it...
- Inside the
bin
folder, create a new file callederrors.rs
. - Add the following code and run it with
cargo run --bin errors
:
1 extern crate futures; 2 3 use futures::prelude::*; 4 use futures::executor::block_on; 5 use futures::stream; 6 use futures::task::Context; 7 use futures::future::{FutureResult, err};
- After that, let's add...