Skip to content

How to propagate error from Timeout::new inside an and_then #132

@p-jackson

Description

@p-jackson

I'm learning about Tokio and was working with some tutorial code like this:

extern crate futures;
extern crate tokio_core;

use std::time::Duration;
use tokio_core::reactor::{Core, Timeout};
use futures::{Future, future};

fn main() {
    let mut core = Core::new().unwrap();

    let handle = core.handle();

    let future = future::ok(())
        .and_then(|_| {
            println!("Wait ...");
            let d = Duration::from_secs(3);

            // How do I propagate this error?
            Timeout::new(d, &handle).unwrap()
        })
        .map(|_| println!("Done"));

    core.run(future).unwrap();
}

How would you avoid unwrapping the result from Timeout::new? Why doesn't Timeout::new just return an immediately failed future if there's a failure during construction? That way you could just return it from the closure directly.

|_| {
    println!("Wait ...");
    let d = Duration::from_secs(3);
    Timeout::new(d, &handle)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions