use futures_util::StreamExt;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::JsFuture;
use web_sys::{console, window, Response};
use wasm_streams::ReadableStream;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = "https://rustwasm.github.io/assets/wasm-ferris.png";
let window = window().unwrap_throw();
let resp_value = JsFuture::from(window.fetch_with_str(url))
.await
.map_err(|_| "fetch failed")?;
let resp: Response = resp_value.dyn_into().unwrap_throw();
let raw_body = resp.body().unwrap_throw();
let body = ReadableStream::from_raw(raw_body.dyn_into().unwrap_throw());
let mut stream = body.into_stream();
while let Some(Ok(chunk)) = stream.next().await {
console::log_1(&chunk);
}
Ok(())
}