这与从 to 移植Qt Webkit
有关Qt Webengine
。以下代码在 webkit 中运行良好。
<script type="text/javascript">
var result = objectExposedFromC++.someFunction(); //sync;
console.log("I want to use result here");
</script>
但是 webengine 的情况发生了变化。
<script type="text/javascript">
var result = objectExposedFromC++.someFunction(); //async;
console.log("I want to use result here, but result isn't avaiable");
</script>
使其工作的一种方法如下所示。
<script type="text/javascript">
var result = objectExposedFromC++.someFunction(function(returnValue){// callback after async return;
console.log("I can use returnValue now.");
}); //async
//other code execute before callback;
</script>
但是js代码有几万,老的浏览器客户端一改就不行了。 我只想对同步调用进行异步调用。
当我找到 时,情况变得更好async/await in ES7
,但这不是解决方案。
<script type="text/javascript">
(async function(){
//async_fucntion will call objectExposedFromC++.someFunction() finally;
var result = await async_fucntion();
console.log("I can use returnValue now.");
})()
</script>
<script type="text/javascript">
(async function(){
//other js code that call async fucntion too.
})()
</script>
```
如你看到的,
(1)我必须使我所有的 js 代码异步,这是同步的,导致订单脚本乱序。
(2)基于 qt webkit 的旧浏览器客户端也不行,因为它可能不支持async/await
.
那么,如何在没有 async/await 支持的情况下等待异步 javascript 函数呢? 有了它我可以
(1) 不需要更改任何现有的 js 代码。浏览器客户端可以注入新的 js 代码来改变异步函数行为。
(2)新浏览器客户端和旧浏览器客户端同时工作。
欢迎任何提示。提前致谢。