【发布时间】:2019-05-12 08:33:57
【问题描述】:
我正在学习 Tokio/futures,但在主函数中找不到将错误返回给调用者的方法;这可能吗?
我的用例是运行时同步的 AWS Lambda,并希望从异步部分返回任何错误。
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,
}
【问题讨论】:
标签: rust rust-tokio