0

This is something related to porting from Qt Webkit to Qt Webengine. The following code works well in webkit.

<script type="text/javascript">
  var result = objectExposedFromC++.someFunction(); //sync;
  console.log("I want to use result here");
</script>

But things changed in webengine.

<script type="text/javascript">
  var result = objectExposedFromC++.someFunction(); //async;
  console.log("I want to use result here, but result isn't avaiable");
</script>

One way to make it work is shown as following.

<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>

But the js code are tens of thouands of, and old browser client won't work once change. I just want to make async call to sync call.

Things become better when I found async/await in ES7, but it isn't a solution.

<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>

```

As you can see,

(1)I must make all my js code async which was sync, leading order scripts out of order.

(2)old browser client based on qt webkit won't work either, because it might not support async/await.

So, how to wait on asynchronous javascript function without async/await support? With that I can

(1) need not to change any existing js code. Browser client can inject new js code to change async fucntion behaviors.

(2) new browser client and old browser client work simutaneously.

Any tips are welcome. Thanks in advance.

waterd
  • 603
  • 1
  • 7
  • 23
  • 1
    If you can't use `await`, use `.then` on the `Promise` – CertainPerformance Nov 19 '18 at 03:44
  • just because something is asynchronous doesn't mean you can assume that a Promise is involved ... asynchrony pre-dates Promises by many many years – Bravo Nov 19 '18 at 03:53
  • 1
    `I just want to make async call to sync call` - if you can achieve this, you will be a god. For how else can a result that will be available at an unknown time in the future be turned into one that is known right now – Bravo Nov 19 '18 at 03:55
  • Use QT Webkit...right? That requires 1) no code change 2) new and old work simultaneously. Fixed. – Randy Casburn Nov 19 '18 at 03:57
  • of course ... async/await can be transpiled (babeljs.io is one example of such a transpiler) into code that will even run on Internet Exploder!! (Internet Exploder being the only mainstream browser that doesn't async/await) – Bravo Nov 19 '18 at 03:57
  • @CertainPerformance Thanks. I edited to clarify my goal just now. Too much work are waiting for me. It won't be my solution so far. I am looking for more possiblities. – waterd Nov 19 '18 at 03:58
  • Your `.someFunction()` method needs to either return a promise (that resolves when the async operation is done) or it needs to accept a callback that will be called when the async operation is complete or it needs to return an object that notifies with some event when the async operation is complete. Those are the three basic choices. If you return a promise, then you can use either `.then()` or `await` on that promise. You seem to already known how to use the callback, but I'd recommend using a promise as it is more flexible. – jfriend00 Nov 19 '18 at 05:56
  • Without seeing the async code for the `.someFunction()` method, we cannot help you with its implementation. FYI, you should never have a function that sometimes behaves synchronously and sometime behaves asynchronously. If it can be either synchronous or asynchronous internally depending upon arguments passed in or internal state, then design it for async and always return the value through the async mechanism. Then, the caller can just program to the async mechanism and it will always work. – jfriend00 Nov 19 '18 at 05:57
  • It seems impossible, and I gave up my perfect goal. I just launched a new web server project for qt/webengine based browser client, and let old web services running until all browser clients update to new one. – waterd Dec 05 '18 at 08:52

0 Answers0