1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-05-23 02:40:13 +02:00
2023-08-17 18:27:20 +02:00

368 B

Error handling for imported Javascript methods

Javascript methods that throw can be imported in Rust like this:

window.throwsIfPair = (num) => {
  if (num % 2 === 0) throw new Error("Pair number");
  return num;
};
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(catch)]
    pub fn throwsIfPair(text: i32) -> Result<i32, JsValue>;
}