I am learning Tokio/futures and can't find a way to return the error up to the caller in the main function; is this possible?
My use case is for AWS Lambda with the runtime being synchronous and would like to return any errors from the async parts.
use futures::Future; // 0.1.26
use reqwest::r#async::Client; // 0.9.14
use reqwest::Error; // 0.1.18
use serde::Deserialize;
use tokio;
fn main() {
let call = synchronous_function();
if let Err(e) = call {
println!("{:?}", e);
}
}
fn synchronous_function() -> Result<(), Error> {
let fut = async_function()
.and_then(|res| {
println!("{:?}", res);
Ok(())
})
.map_err(|_| ());
tokio::run(fut);
Ok(())
}
fn async_function() -> impl Future<Item = Json, Error = Error> {
let client = Client::new();
client
.get("https://jsonplaceholder.typicode.com/todos/1")
.send()
.map_err(Into::into)
.and_then(|mut res| res.json().and_then(|j| Ok(j)))
}
#[derive(Debug, Deserialize)]
struct Json {
userId: u16,
id: u16,
title: String,
completed: bool,
}