Awaiting the future
In the last section, we saw how tasks composed of multiple futures are often difficult to write and debug. One attempt at remedying this is using a crate that wraps futures and associated error handling, yielding a more linear flow of code. This crate is called futures-await and is under active development.
This crate provides two primary mechanisms of dealing with futures:
- The
#[async]
attribute that can be applied to functions, marking them as asynchronous. These functions must return theResult
of their computation as a future. - The
await!
macro that can be used with async functions to consume the future, returning aResult
.
Given these constructions, our earlier example download will look like this:
#[async] fn download(url: &str) -> io::Result<Data> { ... } #[async] fn parse_html(data: &Data) -> io::Result<Links> { ... } #[async] fn process_links(links: &Links) -> io::Result<bool> { ... } #[async] fn download_parse...