Skip to content

Instantly share code, notes, and snippets.

@luciferous
Created January 3, 2012 22:38
Show Gist options
  • Save luciferous/1557343 to your computer and use it in GitHub Desktop.
Save luciferous/1557343 to your computer and use it in GitHub Desktop.
Continuation passing style interface

Continuation passing style interface

Instead of attaching listeners to events, viz.:

    foo = new Foo()
    foo.on "loaded", ->
      bar = new Bar()
      bar.on "loaded", ->
        loadBaz()
      bar.load()
    foo.load()

We pass a function into the method as a return callback:

    loadFoo ->
      loadBar ->
        loadBaz

An additional benefit to this method is composability. If we define a partial application combinator par, where:

    par = (f, x) -> (y) -> f x, y

We can then compose loadBar and loadBaz into one callback it to:

    loadFoo (par loadBar, loadBaz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment