GWT Asynchronous Wait for Callback

37 views Asked by At

Fairly new to GWT and Asynchronous Programming.

Reading through the GWT documentation on RPC and based on that, I have 2 Asynchronous functions that I've implemented in my Java file.

There is a slight dependency between the 2. The idea is that Function A has the capability to use the data generated by Function B, but will still work regardless. This will allow Function A to be a bit flexible in running. To that extend, I made an if-else statement to check whether Function A will use the data from Function B or otherwise.

From what I've seen however, they will both run in parallel with each other, so Function A will process regardless whether Function B is done, resulting in a problematic output. Function B, which does abit more processing, will usually complete only after Function A is completed.

How do I make it so that Function A will not run until Function B is completed, especially if the condition to use the results from Function B is fulfilled?

ExportData export = new ExportData();
final List<?> theResult = new ArrayList<Object>();

            try {
                CommonService.Util.getInstance().FunctionB(<inputData>, new AsyncCallback<List<?>>() {
                    public void onFailure(Throwable caught) {
                        // TODO: Do something with errors.
                        caught.fillInStackTrace();
                      }
                      public void onSuccess(List<?> result){
                        for (Object model : result) { 
                            ((ExportData) theResult).addData((BaseModel)model);
                        }
                      }
                    }
                );

            } catch (ApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            if (<inputData> != null)
            {
                for (Object model : theResult) {
                    export.addData((BaseModel)model);
                }
            }
            else{
                for (BaseModel model : <alternateResults>) {
                    export.addData(model);
                }
            }
            

            CommonService.Util.getInstance().FunctionA(export, WidgetUtils.FILE_DOWNLOAD_CALLBACK);
0

There are 0 answers