This code call the API request sequentially. If each API request takes 1 sec, it takes 10 sec for 10 calls. (I thought this code is concurrent due to await but the result doesnt seem like concurrent. Do I misunderstand Rust await?)
#[tokio::main]
async fn main() {
let client = reqwest::Client::builder().build().unwrap();
loop {
let response = client.get("https://example.com").send().await;
println!("{:?}", thread::current().id());
}
}
However, this JS code call the API concurrently, so as soon as the API returns response, it shows the success message. Not necessarily takes 10 sec for 10 calls.
I wanna call API in Rust like JS (concurrent instead of sequential). I saw some solutions with futures::stream or tokio join, but I dont think I cant use it here due to infinite loop.
How to make it work concurrently like JS code?
when run you will observe the console output alternating between starting request and success.
async isn't just "run things magically in parallel". the await keyword in both languages means "pause execution of this function until the future I'm calling is ready". Their behavior is much more alike than you seem to believe.
P.S. JS and rust are very different languages, I just mean to say that for the examples you posted, they're doing pretty much the same thing.
P.P.S. if you remove the await keyword, then javascript will send multiple requests at once.