Skip to content

Instantly share code, notes, and snippets.

@smathy
Created December 15, 2011 00:53
Show Gist options
  • Save smathy/1479334 to your computer and use it in GitHub Desktop.
Save smathy/1479334 to your computer and use it in GitHub Desktop.
module.exports = (args) ->
_get_callback = (args, pos) ->
if args.length > pos - 1
candidate = args[args.length-pos]
if candidate && typeof candidate == 'function'
candidate
win = ->
fail = ->
if candidate = _get_callback args, 1
win = candidate
if candidate = _get_callback args, 2
fail = win
win = candidate
[ win, fail ]
_calls = require './_calls'
class Bar
foo: (arg='nothing') ->
[ _win, _fail ] = _calls arguments
console.log "Was passed #{arg}"
@bar _win, _fail
bar: ->
[ _win, _fail ] = _calls arguments
console.log "Calling _win"
_win( "Winning" )
console.log "Calling _fail"
_fail( "Failing" )
b = new Bar
console.log "\n# can call with no args"
b.bar()
console.log "\n# can call with a single callback - will be assigned to _win, so the success callback"
b.bar (msg) ->
console.log "Called back with #{msg}"
console.log "\n# can call with two callbacks, first is win, second is fail"
b.bar (msg) ->
console.log "Called back with #{msg}"
, (msg) ->
console.log "Failed out with #{msg}"
console.log "\n# can call with just an arg"
b.foo 'foobar'
console.log "\n# can call with an arg and a single callback"
b.foo 'foobar', (msg) ->
console.log "Called back with #{msg}"
console.log "\n# can also combine both with any args"
b.foo 'foobar'
, (msg) ->
console.log "Called back with #{msg}"
, (msg) ->
console.log "Failed out with #{msg}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment