title: How to handle Rust's errors from Result as a return value instead of throwing an exception in JavaScript?
body: <p>I'd like to write JavaScript like:</p>
  
  <pre class="lang-js prettyprint-override"><code>var [greeting, err] = wasm.hello("world");
  </code></pre>
  
  <p>It appears that if I use the standard <code>Result</code> type, then I need to use a <code>try</code>/<code>catch</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>try {
    greeting = wasm.hello()
  }
  catch(error) {
    console.error(error);
  }
  </code></pre>
  
  <p>Rust code that generates the error: </p>
  
  <pre class="lang-rust prettyprint-override"><code>#[wasm_bindgen]
  pub fn hello(name: &amp;str) -&gt; Result&lt;String, JsValue&gt; {
      if name == "" {
          return Err(JsValue::from("name required!"));
      }
  
      let greeting = format!("Hello {}!", name);
  
      Ok(greeting)
  }
  </code></pre>
  
  <p>This is based on the example in <a href="https://github.com/rustwasm/rust-parcel-template" rel="nofollow noreferrer">rust-parcel-template</a>. The full webapp with Rust + WASM + JS with the little example above is on <a href="https://github.com/ultrasaurus/hello-rust-parcel/blob/hello/js/index.js" rel="nofollow noreferrer">GitHub</a>.  </p>