Search code examples
javascriptprototypecallback

Callback when method finished


I want to execute method: bar() when method foo() is done.

This could be done like this:

function foo() {
  //....
  bar();
}

But there is surely some way to execute bar() like a callback to foo()? I have been looking in prototype API, but so far I only found ways to bind callbacks to HTML-elements...


Solution

  • function foo(callback) {
       // ...
       callback();
    }
    

    And then you call foo like this:

    foo(bar);