diff --git a/crates/futures/src/lib.rs b/crates/futures/src/lib.rs index 7bd5c9b207b..3711a66662b 100644 --- a/crates/futures/src/lib.rs +++ b/crates/futures/src/lib.rs @@ -377,3 +377,15 @@ fn _future_to_promise(future: Box>) -> P } } } + +/// Spawns a future. +pub fn spawn_local(future: F) +where + F: Future + 'static, +{ + future_to_promise( + future + .map(|_| JsValue::undefined()) + .map_err(|_| JsValue::undefined()), + ); +} diff --git a/crates/futures/tests/tests.rs b/crates/futures/tests/tests.rs index 87d8a1f46fb..7b76dfe5b2a 100644 --- a/crates/futures/tests/tests.rs +++ b/crates/futures/tests/tests.rs @@ -9,7 +9,7 @@ extern crate wasm_bindgen_test; use futures::unsync::oneshot; use futures::Future; use wasm_bindgen::prelude::*; -use wasm_bindgen_futures::{future_to_promise, JsFuture}; +use wasm_bindgen_futures::{future_to_promise, spawn_local, JsFuture}; use wasm_bindgen_test::*; #[wasm_bindgen_test(async)] @@ -68,3 +68,21 @@ fn oneshot_works() -> impl Future { closure.forget(); rx.then(|_| Ok(())) } + +#[wasm_bindgen_test(async)] +fn spawn_local_runs() -> impl Future { + let (tx, rx) = oneshot::channel::(); + let fn_box = Box::new(move || { + tx.send(42).unwrap(); + }); + spawn_local(futures::future::ok::<(), ()>(()).map(|_| { + fn_box(); + })); + rx.then(|val| { + if val == Ok(42) { + Ok(()) + } else { + Err(JsValue::undefined()) + } + }) +}