Last active
August 29, 2015 14:22
-
-
Save royhowie/f88b69c3465320fadc84 to your computer and use it in GitHub Desktop.
how to use callbacks according to NPM style guides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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