Skip to content

Add Future::delay #341

New issue

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

By clicking “No 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? No Sign in to your account

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

Add Future::delay #341

yoshuawuyts opened this issue Oct 15, 2019 · 0 comments · Fixed by #349
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@yoshuawuyts
Copy link
Contributor

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);
@yoshuawuyts yoshuawuyts added enhancement New feature or request good first issue Good for newcomers labels Oct 15, 2019
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant