Skip to content

Instantly share code, notes, and snippets.

@joepie91
Created April 7, 2015 11:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepie91/98576de0fab7badec167 to your computer and use it in GitHub Desktop.
Save joepie91/98576de0fab7badec167 to your computer and use it in GitHub Desktop.
Predictable sync vs. async in Node.js
var i = 0;
maybeSyncMaybeAsync(function(){
i++;
});
console.log("The value of i is", i);
@saraf
Copy link

saraf commented Jul 29, 2015

to make this into a runnable example - async and sync versions of the function could be added -

//First try this
//This is asynchronous execution of the callback
function maybeSyncMaybeAsync(cb) {
        process.nextTick(cb);
}

/*
//Then try this definition
//This is synchronous
function maybeSyncMaybeAsync(cb) {
        cb();
}
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment