title: In iOS 13 and below, how can WKWebView call an async Javascript function (i.e. one that returns a promise)?
body: <p>iOS 14 has <a href="https://developer.apple.com/documentation/webkit/wkwebview/3656441-callasyncjavascript" rel="nofollow noreferrer">this API</a>, however how can this be achieved in iOS 13 or below?</p>
  <p>For example, suppose the JS has a function defined as:</p>
  <pre><code>function upperCaseAsync(text) {
    return new Promise((resolve, reject) =&gt; {
      setTimeout(() =&gt; {
        resolve(text.toUpperCase());
      }, 300);
    });
  }
  </code></pre>
  <p>How to call and handle the resulting promise's result? I tried using the <a href="https://developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript" rel="nofollow noreferrer">synchronous API</a>, but it throws an error:</p>
  <blockquote>
  <p>Error Domain=WKErrorDomain Code=5 &quot;JavaScript execution returned a result of an unsupported type&quot;</p>
  </blockquote>