-1

I have to call a javacript function that returns some data(which I want) in a callback function. Now currently I am transferring back this data to flex by calling it from javascript.

But I want the flex to somehow wait for the javascript function to get data. How can I do that?

Code looks something like:

This is my flex call to javascript :

function delegateComputaionToJavascript(uncomputedData){
    externalInterface.call('myJavascriptFunctionThatReturnsCallback', uncomputedData);

}
function computationCompleteHandler(computedData){
    //goes ahead to process further
    //like saving this data to DB in a server call
}

My javascript function that returns data:

function myJavascriptFunctionThatReturnsCallback(uncomputedData){
    var SomeDataComputator= SomeDataComputator();
    SomeDataComputator.computeData(uncomputedData, function (computedData){
        // call the flex again from here
        myFlashObject.computationCompleteHandler(computedData); 
 })
}

What I want is that delegateComputaionToJavascript function in flex should not only call the javascript but wait for myJavascriptFunctionThatReturnsCallback to finish execution so that I can make a server call in delegateComputaionToJavascript itself.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
  • @Ivar it doesn't talks about actionscript anywhere. – Shanu Gupta Aug 24 '17 at 11:50
  • You ask for flex to wait until the JavaScript function has the data. That can be solved by using a callback that calls the flex when it is finished. I don't know how you get the data in the callback function since you don't specify that. – Ivar Aug 24 '17 at 11:51
  • @Ivar for simplicity the data is computed in javascript itself. Since it needs some time for that, timeout is used. That's why the data I need comes in a callback. – Shanu Gupta Aug 24 '17 at 12:02
  • But doesn't that callback only fire once it has the data? Can't you call the flex from that callback so it will only be called once that data is available? – Ivar Aug 24 '17 at 12:03
  • That what I am doing currently. Please check the samples I added to make it clearer. – Shanu Gupta Aug 24 '17 at 12:17
  • Downvoter care to explain? – Shanu Gupta Aug 25 '17 at 08:45
  • 1
    How do you know when your JS `myJavascriptFunctionThatReturnsCallback` is actually finished? Why can't that "I'm finished" part start running the AS3 `computationCompleteHandler` function? – VC.One Aug 25 '17 at 09:39

1 Answers1

0

Cant say anything about flex since I've never worked with it closely, but in pure as3 you can get the return value from javascript as follows:

const returnValue: * = ExternalInterface.call(...);

Any concrete solution will strongly depend on your existing code.

Nbooo
  • 865
  • 7
  • 13
  • Well, your js code is based on the callbacks as well. Either you make the whole thing async (i.e. leave it as is with callbacks) or compute the value and return it in a synchronous way. – Nbooo Aug 24 '17 at 13:19