Created
January 24, 2017 16:37
-
-
Save eighteyes/026267771dfc717557a8ccb2b7b42eba to your computer and use it in GitHub Desktop.
Newb to node?
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
// procedural programming - synchronous | |
var foo = 'foo'; | |
foo += 'bar'; | |
// foo = 'foobar' | |
// object oriented programming - synchronous | |
var Bar = function(a){ return a } | |
var foo = new Bar({ id: 123 }) | |
foo.id = 123; | |
// functional programming - potentially asynchronous | |
var callback = function(error, data){ | |
if (error) { return console.error(error); | |
data.callbackAdded = true; | |
return data; | |
} | |
var bar = function(cb){ | |
var good = true; | |
if (!good){ | |
cb('not good'); | |
} else { | |
cb( null, { | |
barAdded : true | |
}) | |
} | |
} | |
var foo = bar( callback ); | |
// foo = { barAdded: true, callbackAdded: true } | |
// don't do | |
var foo = bar( callback() ); | |
// the () is what activates the function, and it will execute before the container (bar) function | |
// script1 | |
//synchronous is blocking | |
var database = getDatabaseSync(); | |
console.log( 'a', timer ) | |
// big difference ^v^v^v^ | |
// script2 | |
// asynchronous is not | |
getDatabase( function(err, db) { | |
console.log('b', timer) | |
db.connect(...); | |
}) | |
console.log('c', timer); | |
// if run simultaneously, script 2 'c' would fire before a or b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment