Support passing `FnOnce` to JS
Motivation
We currently have support for Closure<FnOnce>, but not passing FnOnce directly to JS.
The Promise::new function currently accepts an FnMut, but the closure will only be called once, so it should be FnOnce instead.
This has practical implications. Right now the wasm-bindgen-futures code looks like this:
let mut future = Some(future);
Promise::new(&mut |resolve, reject| {
let future = future.take().unwrap_throw();
...
})
But if it were FnOnce we could simplify that to this:
Promise::new(move |resolve, reject| {
...
})
FWIW, we have https://docs.rs/wasm-bindgen/0.2.50/wasm_bindgen/closure/struct.Closure.html#method.once
@fitzgen Yeah, I mentioned that above, but that's not what this issue is about.
(Though it might be able to reuse some parts of the implementation of Closure::once)
Any updates on this?
From the linked item #3550, I wanted to point out that the deeper the nesting of the closures, the uglier this gets from an ownership perspective. Having Promise::new accept an FnOnce resolves a ton of ownership complexity.
I'm curious whether users are typically building JS Promises directly via Promise::new or if they're using Rust futures and then converting them to Promises via wasm_bindgen_futures::future_to_promise?
Probably most users use future_to_promise() as it's far simpler to use.