Skip to content

Instantly share code, notes, and snippets.

@royhowie
Last active August 29, 2015 14:22
Show Gist options
  • Save royhowie/f88b69c3465320fadc84 to your computer and use it in GitHub Desktop.
Save royhowie/f88b69c3465320fadc84 to your computer and use it in GitHub Desktop.
how to use callbacks according to NPM style guides
// in your library.js file
module.exports = {
getFibonacciNumber: function (n, done) {
if (n < 0) {
return done("Can only calculate fibonacci numbers for an integer n > -1 !");
} else {
var a = 0
, b = 1
, i = 0
, holder
;
while (i++ < n) {
holder = a;
a = b;
b = a + b;
}
return done(null, b);
}
}
}
// somewhere else
var library = require("library")
var nth = 25;
library.getFibonacciNumber(nth, function (err, n) {
if (err) {
console.log(err);
} else {
console.log("The " + nth + "th Fibonacci number is " + n);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment