1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// fetchmod.rs
//region description
//! ## Async world
//! With the new async/.await syntax in Rust it is now very easy to write async code.
//! It is important to be careful to NOT write sync code after async code.
//! It can be confusing that the sync code will execute before the async code !
//! Webassembly is basically javascript and uses the recommended executor spawn_local() or future_to_promise().
//! Async cannot use any references to the stack because it is executed in another timeline.
//endregion

//region: use
use crate::*;

use unwrap::unwrap;
use wasm_bindgen::{JsValue, JsCast};
use web_sys::{Request, RequestInit, Response};
use wasm_bindgen_futures::{JsFuture};
//endregion

/// fetch in Rust with async await for executor spawn_local()
/// return the response as JsValue. Any error will panic.
pub async fn async_spwloc_fetch_text(url: String) -> String {
    //Request init
    let mut opts = RequestInit::new();
    opts.method("GET");
    let request = unwrap!(Request::new_with_str_and_init(&url, &opts));
    let resp_jsvalue =
        unwrap!(JsFuture::from(utilsmod::window().fetch_with_request(&request)).await);
    let resp: Response = unwrap!(resp_jsvalue.dyn_into());
    let resp_body_text = unwrap!(JsFuture::from(unwrap!(resp.text())).await);
    //logmod::debug_write(&unwrap!(JsValue::as_string(&resp_body_text)));
    //returns response as String
    unwrap!(JsValue::as_string(&resp_body_text))
}

/// fetch in Rust with async await for executor spawn_local()
/// return the response as String. Any error will panic.
pub async fn fetch_response(url: String) -> String {
    //Request init
    let mut opts = RequestInit::new();
    opts.method("GET");
    let request = unwrap!(Request::new_with_str_and_init(&url, &opts));
    //log1("before fetch");
    let resp_jsvalue =
        unwrap!(JsFuture::from(utilsmod::window().fetch_with_request(&request)).await);
    //log1("after fetch");
    let resp: Response = unwrap!(resp_jsvalue.dyn_into());
    //log1("before text()");
    let text_jsvalue = unwrap!(JsFuture::from(unwrap!(resp.text())).await);
    let txt_response: String = unwrap!(text_jsvalue.as_string());
    //logmod::debug_write(&txt_response);
    //returns response as String
    txt_response
}

/// fetch only, so it goes in cache
pub async fn fetch_only(url: String) {
    //Request init
    let mut opts = RequestInit::new();
    opts.method("GET");
    let request = unwrap!(Request::new_with_str_and_init(&url, &opts));
    //log1("before fetch");
    unwrap!(JsFuture::from(utilsmod::window().fetch_with_request(&request)).await);
}