54
loading...
This website collects cookies to deliver better user experience
async
and await
syntax in JavaScript and Rust code:// greet.js
async function main() {
await greet("Hi there!");
}
async function greet(message) {
console.log(message);
}
/*
Output:
Hi there!
*/
// greet.rs
#[tokio::main]
async fn main() {
greet("Hi there!").await;
}
async fn greet(message: &str) {
println!("{}", message);
}
/*
Output:
Hi there!
*/
publish
function, which returns a Promise, without needed to await
it. setTimeout
queues the given callback on the event loop and immediately returns a Promise
. The main
function then logs "Don't block me!" to the console. Finally the event loop eventually runs our callback, which logs "Executed main()".// publish.js
async function main() {
publish("Executed main()"); // no await
console.log("Don't block me!");
}
// returns a Promise
function publish(message) {
return setTimeout(() => {
console.log(message);
}, 0);
}
/*
Output:
Don't block me!
Executed main()
*/
await
the Future
returned by publish
.// publish.rs
#[tokio::main]
#[allow(unused_must_use)]
async fn main() {
publish("Executed main()"); // no await
println!("Don't block me!");
}
async fn publish(message: &str) {
println!("{}", message);
}
/*
Output:
Don't block me!
*/
Future
, "futures do nothing unless you .await
them". Unlike JavaScript, Rust's async operations are lazy..await
on the Future
to execute the async operation. However awaiting the publish
causes the main
function to suspend and prevent execution of println!("Don't block me!")
until after publish
completes. To avoid this situation and achieve a similar behaviour to JavaScript's Promise handling, we can use tokio::spawn
.// publish.rs
#[tokio::main]
async fn main() {
// Spawns a new async task, and executes it concurrently
tokio::spawn(async {
publish("Executed main()").await;
});
println!("Don't block me!");
}
async fn publish(message: &str) {
println!("{}", message);
}
/*
Output:
Don't block me!
Executed main()
*/
tokio::spawn
executes publish
concurrently without blocking the remaining lines operations in main
!