Skip to content

Instantly share code, notes, and snippets.

@dhruvbird
Created June 13, 2011 11:20
Show Gist options
  • Save dhruvbird/1022620 to your computer and use it in GitHub Desktop.
Save dhruvbird/1022620 to your computer and use it in GitHub Desktop.
DNS callbacks are synchronous at times
var dns = require('dns');
console.log('Calling resolveSrv(localhost)');
dns.lookup('localhost', function() {
console.log('Callback for resolve(localhost)');
});
console.log('After calling resolveSrv(localhost)');
console.log('Calling resolveSrv(127.0.0.1)');
dns.lookup('127.0.0.0', function() {
console.log('Callback for resolveSrv(127.0.0.1)');
});
console.log('After calling resolve(127.0.0.1)');
Calling resolveSrv(localhost)
After calling resolveSrv(localhost)
Calling resolveSrv(127.0.0.1)
After calling resolve(127.0.0.1)
Callback for resolve(localhost)
Callback for resolveSrv(127.0.0.1)
(btw, this is what happens if lookup() is replaced with resolve())
Calling resolveSrv(localhost)
Callback for resolve(localhost)
After calling resolveSrv(localhost)
Calling resolveSrv(127.0.0.1)
Callback for resolveSrv(127.0.0.1)
After calling resolve(127.0.0.1)
@dhruvbird
Copy link
Author

Yes, the callbacks to lookup are at times called before the function completes. I know that there is no guarantee of when the callback is actually called, but was wondering if it made sense to just guarantee that callbacks (if any) will surely be called after the complete function (caller) is finished executing. Just feel so because I've run into a bug where the author assumed otherwise and I had to debug for nearly 4 hrs.

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