Clean way in GWT/Java to wait for multiple asynchronous events to finish

前端 未结 8 871
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:32

What is the best way to wait for multiple asynchronous callback functions to finish in Java before continuing. Specifically I\'m using GWT with AsyncCallback, but I think this i

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 14:08

    Ideally, you want to do as other posters have stated and do as much as you can in a single async call. Sometimes you have to do a bunch of separate calls. Here's how:

    You want to chain the async calls. When the last async completes (login), all the items are loaded.

        final AsyncCallback loginCallback = new AsyncCallback() {
            public void onSuccess(LoginInfo result) {
                //Everything loaded
                doSomethingNow();
            }
        };
        final Runnable searchRunnable = new Runnable() {
            public void run() {
                loginService.login(GWT.getHostPageBaseURL(), loginCallback);
            }
        };
    
        final Runnable booksRunnable = new Runnable() {
            public void run() {
                AjaxLoader.loadApi("search", "1", searchRunnable, null);
            }
        };
    
        //Kick off the chain of events
        AjaxLoader.loadApi("books", "0", booksRunnable, null);
    

    Cheers,

    --Russ

提交回复
热议问题