Skip to content

Instantly share code, notes, and snippets.

@ziyan-junaideen
Created January 30, 2014 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziyan-junaideen/8717925 to your computer and use it in GitHub Desktop.
Save ziyan-junaideen/8717925 to your computer and use it in GitHub Desktop.
Making callback functions safe - I mean not fail catastrophically because not providing a function or array of functions
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 () ->
// Generated by CoffeeScript 1.6.3
(function() {
window.SafeCallback = (function() {
function SafeCallback() {}
SafeCallback.arrayCallback = function(callback) {
var item, result, _i, _len;
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 (_i = 0, _len = callback.length; _i < _len; _i++) {
item = callback[_i];
if (typeof item === 'function') {
result.push(item);
}
}
return result;
}
console.log("SafeCallback: error with callback...");
return function() {};
};
SafeCallback.callback = function(callback) {
if (typeof callback === 'function') {
return callback;
}
return function() {};
};
return SafeCallback;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment