Skip to content

Instantly share code, notes, and snippets.

@gregorynicholas
Forked from dypsilon/callback_pattern.js
Created September 28, 2019 16:13
Show Gist options
  • Save gregorynicholas/ad9f6f0ca65d00ae1a4c1cc0c720d518 to your computer and use it in GitHub Desktop.
Save gregorynicholas/ad9f6f0ca65d00ae1a4c1cc0c720d518 to your computer and use it in GitHub Desktop.
The callback pattern found in many node.js functions.
var invokee1 = function(err, callback) {
// cant change this function
callback();
};
var invokee2 = function(err, optional1, optional2, callback) {
// work with optional 1 or optional 2
console.log(optional1);
console.log(optional2);
// cant change this function
callback();
};
var invoker = function(invokee) {
var cb = function() {
console.log('this is the callback');
};
// call the invokee with the right amount of arguments
var args = [null, 'a', 'b', 'c', 'd']; // null is the error arg
args[invokee.length - 1] = cb;
invokee.apply(null, args);
};
invoker(invokee1);
invoker(invokee2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment