Async communication of swift and Javascriptcore
<p>I want to use the Async functionality offered by WKWebView outside web view. 
The JS Context option does not provide the Async functionality.</p>

<p>In WKWebView, I write my logic as follows.</p>

<pre><code>func swiftFunc1() {
   webView.evaluateJavaScript("jsFunc1(), completionHandler: nil)
}
</code></pre>

<p>In javascript code I post a message to swift</p>

<pre><code>function jsFunc1() {
    window.webkit.messageHandlers.myMsg.postMessage("call swiftFunc2");
}
</code></pre>

<p>The swift code can then call appropriate JS callback as a part of handling message. </p>

<p>But this is dependent upon the webview being the foreground view. If I want to use the JS logic independent of the webview, JSContext is the option. I tried following</p>

<pre><code>func swiftFunc1() {
    myCtxt = JSContext()
    exportedToJS = exportToJS() //confirms to JSExport and uses @objc
    myCtxt.setObject(exportedToJS.self, forKeyedSubscript: "swiftIface")
    myFunc = myCtxt.objectForKeyedSubscript("jsFunc1")
    myFunc.callWithArguments(nil)
}
</code></pre>

<p>Now in javascript code I cannot post a message to swift. If I try to call a swift function as follows, code gets stuck forever.</p>

<pre><code>function jsFunc1() {
   swiftIface.swiftFunc2() // This creates a deadklock
 }
</code></pre>

<p>How can I achieve either of the following without "returning" from the called Javascript function jsFunc1()?</p>

<ol>
<li><p>Either post a message to swift so that it can take appropriate action</p></li>
<li><p>Or call a swift function so that the appropriate action is taken</p></li>
</ol>