Skip to content

Instantly share code, notes, and snippets.

@makenova
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makenova/10772450 to your computer and use it in GitHub Desktop.
Save makenova/10772450 to your computer and use it in GitHub Desktop.
JS callback async confusion
get('dog', function(err, dog){
if (err){ throw (err); }
get('bone', function(err, bone){
// The following variable in intentionally global.
praise = dog + ' is a good boy, he deserves a ' + bone + '.';
console.log('the praise is set.');
});
});
// I would think the following line would throw an error because
// praise wouldn't be defined by the time it gets here.
// when the first async methd is executed(get 'bone'), isn't
// this the next line that is executed?
console.log( praise );
function get(item, callback){
console.log('getting ', item);
var items = {bone: 'milk bone', dog: 'Roger R. Woof-woof'};
return callback(null, items[item]);
}
// Console output from running this file
// $ node goodboy.js
// getting bone
// getting dog
// the praise is set.
// Roger R. Woof-woof is a good boy, he deserves a milk bone.
// Adding a delay on the get function porduces the expected Reference error
get('dog', function(err, dog){
if (err){ throw (err); }
get('bone', function(err, bone){
praise = dog + ' is a good boy, he deserves a ' + bone + '.';
console.log('the praise is set.');
});
});
console.log( praise );
function get(item, callback){
console.log('getting ', item);
setTimeout(function(){
var items = {bone: 'milk bone', dog: 'Roger R. Woof-woof'};
return callback(null, items[item]);
}, 0);
}
// Console output from running this file
// $ node goodboyfail.js
// getting dog
//
// /Users/makenova/Desktop/b75a2672f497fa864a1f/goodboyfail.js:11
// console.log( praise );
// ^
// ReferenceError: praise is not defined
// at Object.<anonymous> (/Users/makenova/Desktop/b75a2672f497fa864a1f/goodboyfail.js:11:14)
// at Module._compile (module.js:456:26)
// at Object.Module._extensions..js (module.js:474:10)
// at Module.load (module.js:356:32)
// at Function.Module._load (module.js:312:12)
// at Function.Module.runMain (module.js:497:10)
// at startup (node.js:119:16)
// at node.js:902:3

What makes a JS function async?

Is it possible to write a custom asynchronous function without the use of JS's timer functions such as setTimeout and setInterval ?

I have two code examples below, one that succeeds(goodboy.js) and one that fails(goodboyfail.js). I originally thought, goodboy.js would fail because I assumed

  1. Simply defining get with a callback function would make it asyncronous
  2. praise would be undefined when it was executed.

However, the console.log messages seem to indicate that the get function executes syncronously. I got the expected behviour in goodboyfail.js by adding a delay.

Notes

  1. I've ignored error handling
  2. I am aware that the 0ms applied to the setTimeout(goodboyfail.js) is not actually a 0 second delay but will be the browser minimum ~4ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment