Future chaining

You can try

fn data_saving(data: serde_json::Value) -> Box<Future<Item=hyper::Response, Error=hyper::Error>> {
    let addr = env::args()
        .nth(1)
        .unwrap_or("127.0.0.1:6379".to_string())
        .parse()
        .unwrap();
    let db_connection = client::paired_connect(&addr);
    Box::new(db_connection.and_then(|connection| {
        connection.send::<redis_async::resp::RespValue>(resp_array!["SET", "key", "data"])
    }).then(|result| match result {
        Ok(result) => {
            futures::future::ok(Response::new().with_status(StatusCode::InternalServerError))
        }
        Err(e) => {
            println!("Unexpected error: {:?}", e);
            futures::future::ok(Response::new().with_status(StatusCode::InternalServerError))
        }
    }))
}

Or:

fn data_saving(data: serde_json::Value) -> impl Future<Item=hyper::Response, Error=hyper::Error> {
    let addr = env::args()
        .nth(1)
        .unwrap_or("127.0.0.1:6379".to_string())
        .parse()
        .unwrap();
    let db_connection = client::paired_connect(&addr);
    db_connection.and_then(|connection| {
        connection.send::<redis_async::resp::RespValue>(resp_array!["SET", "key", "data"])
    }).then(|result| match result {
        Ok(result) => {
            futures::future::ok(Response::new().with_status(StatusCode::InternalServerError))
        }
        Err(e) => {
            println!("Unexpected error: {:?}", e);
            futures::future::ok(Response::new().with_status(StatusCode::InternalServerError))
        }
    })
}
2 Likes