Hello,

as everything Ajax-related is (mostly) asynchronous every response is
handled using callbacks.
I often have the problem that to do action A I have to initialize
multiple components on the page (if not initialized yet), then fire 1-
n ajax calls, waiting for the callback and so on.
this leads to a set of functions chained together by callback-calls.

function action() {

  if (!$('#comp1').is('.loaded')) {
   $('#comp1').trigger('load.comp1', [ cb1 ])
  }

  if (!$('#comp2').is('.loaded')) {
   $('#comp2').trigger('load.comp2', [ cb2 ])
  }

  if (!$('#comp3').is('.loaded')) {
   $('#comp3').trigger('load.comp3', [ cb3 ])
  }

  if (cb1 &&  cb2 && cb3 have been called and returned success) {
    action2(action2-callback)

    if (action2-callback has been called and returned success) {
        // do real business logic
    }
  }
}

do you know of any patterns to implement these callback-chains more
elegantly?
how do you solve nested, nasty callback-chains?

Reply via email to