出于好奇,我在玩wasm-bindgen
(https://github.com/rustwasm/wasm-bindgen)。
[在玩Navigator
(web_sys
板条箱]时,我偶然发现了此方法:
https://docs.rs/web-sys/0.3.36/web_sys/struct.MediaDevices.html#method.enumerate_devices
它返回一个Result<Promise, JsValue>
..现在,我是Rust的新手,我的问题是如何获取Promise
的值?
Closure::wrap
如何工作?如何与then方法一起使用以获取结果?
[我想知道是否有人会这样向我解释如何处理Promise
这里是一个返回Promise
的示例:
let window = web_sys::window().expect("no global `window` exists");
let navigator = window.navigator();
if let Ok(devs) = navigator.media_devices() {
if let Ok(prom) = devs.enumerate_devices() {
//..??? how to list all devices
}
}
祝您一切顺利,路卡
有official documentation available on the wasm-bindgen site。
总之,您可以使用wasm_bindgen_futures::JsFuture::from(promise).await?
来检索Promise的结果,并继续使用常规的Rust async
功能。