Macro tokio_js_set_interval::set_timeout [−][src]
macro_rules! set_timeout { ($cb:ident, $ms:literal) => { ... }; (|| $cb:expr, $ms:literal) => { ... }; (move || $cb:expr, $ms:literal) => { ... }; ($cb:expr, $ms:literal) => { ... }; ($cb:block, $ms:literal) => { ... }; }
Expand description
Creates a timeout that behaves similar to setTimeout(callback, ms)
in Javascript
for the tokio
runtime. Unlike in Javascript, it will only be executed, if after the
specified time passed, the tokio
runtime still lives, i.e. didn’t got dropped.
As in Javascript, a timeout may only have side effects and no return type. You don’t get a handle to manually wait for it, you must ensure, that the tokio runtime lives long enough.
Parameters
#1
expression, closure-expression, block or identifier (which points to a closure). The code that represents the callback function.#2
time delay in milliseconds
Example
use tokio::time::Duration; use tokio_js_set_interval::set_timeout; #[tokio::main] async fn main() { set_timeout!(println!("hello1"), 0); println!("hello2"); // prevent that tokios runtime gets dropped too early // order of output should be // "hello2" // "hello1" tokio::time::sleep(Duration::from_millis(1)).await; }