diff --git a/examples/loop_yield.rs b/examples/loop_yield.rs index 472a916..783ec19 100644 --- a/examples/loop_yield.rs +++ b/examples/loop_yield.rs @@ -42,7 +42,7 @@ async fn common_code() -> Result<(), Box> { let (tx, rx) = futures::channel::oneshot::channel(); fetch( request, - move |result: Result| { + move |result: Result| async { tx.send(result.expect("Expecting Response not Error").status()) .expect("Receiver should still be available"); }, diff --git a/examples/simple_fetch.rs b/examples/simple_fetch.rs index b443ede..1dedd86 100644 --- a/examples/simple_fetch.rs +++ b/examples/simple_fetch.rs @@ -26,7 +26,7 @@ async fn common_code() -> Result<(), Box> { fetch( request, - move |result: Result| { + move |result: Result| async { tx.send(result.expect("Expecting Response not Error").status()) .expect("Receiver should still be available"); }, diff --git a/src/wrappers.rs b/src/wrappers.rs index c128f05..101eff7 100644 --- a/src/wrappers.rs +++ b/src/wrappers.rs @@ -1,6 +1,7 @@ //! Stores the wrapper functions that can be called from either native or wasm //! code +#[cfg(not(target_arch = "wasm32"))] /// Performs a HTTP requests and calls the given callback when done. NB: Needs /// to use a callback to prevent blocking on the thread that initiates the /// fetch. Note: Instead of calling get like in the example you can use post, @@ -19,7 +20,7 @@ /// let request = client.get("http://httpbin.org/get"); /// let (tx, rx) = futures::channel::oneshot::channel(); /// -/// fetch(request, move |result: Result| { +/// fetch(request, move |result: Result| async { /// tx.send(result.expect("Expecting Response not Error").status()) /// .expect("Receiver should still be available"); /// }); @@ -32,13 +33,28 @@ /// # #[cfg(target_arch = "wasm32")] /// # fn main(){} /// ``` -pub fn fetch(request: reqwest::RequestBuilder, on_done: F) +pub fn fetch(request: reqwest::RequestBuilder, on_done: F) +where + F: 'static + Send + FnOnce(reqwest::Result) -> O, + O: futures::Future + Send, +{ + let future = async move { + let result = request.send().await; + on_done(result).await; + }; + spawn(future); +} + +/// dox +#[cfg(target_arch = "wasm32")] +pub fn fetch(request: reqwest::RequestBuilder, on_done: F) where - F: 'static + Send + FnOnce(reqwest::Result), + F: 'static + FnOnce(reqwest::Result) -> O, + O: futures::Future, { let future = async move { let result = request.send().await; - on_done(result) + on_done(result).await; }; spawn(future); } diff --git a/tests/web.rs b/tests/web.rs index 4df2377..a12a0f6 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -12,7 +12,7 @@ fn main() { fetch( request, - move |result: Result| { + move |result: Result| async { tx.send(result.expect("Expecting Response not Error").status()) .expect("Receiver should still be available"); },