Skip to content

Instantly share code, notes, and snippets.

@andrematias
Created August 17, 2020 19:14
Show Gist options
  • Save andrematias/e7e59386f6e4ddfbce8754b410f24404 to your computer and use it in GitHub Desktop.
Save andrematias/e7e59386f6e4ddfbce8754b410f24404 to your computer and use it in GitHub Desktop.
This gist is part of the book Learning Node by Shelley Powers, Novatec 2017
const fb = (n) => {
if(n<2) return n;
return fb(n -1) + fb(n -2);
}
const Obj = function(){};
Obj.prototype.doSomething = function(arg1_){
const callback_ = arguments[arguments.length - 1]; //This word 'arguments' just works with anonymous function
callback = (typeof(callback_) == 'function' ? callback_ : null);
const arg1 = typeof arg1_ === 'number' ? arg1_ : null;
if(!arg1)
return callback(new Error('first arg missing or not a number'));
process.nextTick(() => {
//this point will block the CPU
const data = fb(arg1);
callback(null, data);
});
}
const test = new Obj();
test.doSomething(10, function(err, value){
if(err)
console.error(err);
else
console.log('fibonaci value for %d is %d', 10, value);
});
console.log('called doSomething');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment