Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Future::delay #341

Closed
yoshuawuyts opened this issue Oct 15, 2019 · 0 comments · Fixed by #349

Comments

@yoshuawuyts
Copy link
Member

@yoshuawuyts yoshuawuyts commented Oct 15, 2019

Similar to #309 we should add a method to Future that delays execution for a Duration. This method should be marked as "unstable".

This is useful in some cases where you want to define a Future that has a delay, rather than combining task::sleep with an async block and another future.

Examples

use async_std::future;

let a = future::ready(1).delay(Duration::from_millis(200));
let b = future::ready(2).delay(Duration::from_millis(100));
let c = future::ready(3).delay(Duration::from_millis(300));

dbg!(future::join!(a, b, c).await);

This is simlilar to this, but less nice to write:

let a = async {
    task::sleep(Duration::from_millis(200).await;
    future::ready(1).await
};
let b = async {
    task::sleep(Duration::from_millis(100).await;
    future::ready(2).await
};
let c = async {
    task::sleep(Duration::from_millis(300).await;
    future::ready(3).await
};

dbg!(future::join!(a, b, c).await);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.