public
Created

Making callback functions safe - I mean not fail catastrophically because not providing a function or array of functions

  • Download Gist
safe_callback.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class window.SafeCallback
 
@arrayCallback: ( callback ) ->
if typeof callback == 'function'
console.log "SafeCallback: expected array, found function..."
return [ callback ]
 
if callback instanceof Array
console.log "SafeCallback: filtering functions from array..."
result = []
for item in callback
result.push item if typeof item == 'function'
return result
 
console.log "SafeCallback: error with callback..."
return () ->
 
@callback: ( callback ) ->
return callback if typeof callback == 'function'
 
return () ->

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.