I am trying to download a few files inside a loop:
let j = reqwest::get(&link.api)
.await
.expect("HTTP GET error")
.json::<RestResp>()
.await
.expect("Deserialization error");
for post in j.posts {
match (post.tim, post.ext) {
(Some(tim), Some(ext)) => {
let file_name = format!("{}{}", tim, ext);
let uri = format!("https://i.4cdn.org/{}/{}", link.board, file_name);
download(&uri, &file_name, dir.clone());
}
_ => {}
}
}
Now each of the download
function returns a future. How can I tell the compiler to wait until all the futures created from inside a loop?
In Go, I would create a WaitGroup
and wg.Add(1)
after each call to download. Inside the download()
function, when a download finishes I would send wg.Done()
signal to indicate that that the process finished. And before main()
ends, I would call wg.Wait()
to instruct the program to wait for all waitgroups to finish. I'm not sure how to achieve that in async-await.