misc/eventloop-illustration/
Error-first callback in Node.js says: "I am asynchronous!"
var https = require('https');
function getHeadersHTTPS(address, cb){
https.get(address)
.on('response', function(response){
//success! pass `null` error + headers to callback
cb(null, response.headers);
})
.on('error', function(err){
//error :( pass to callback
cb(err);
});
}
var target = 'https://devdocs.io/';
getHeadersHTTPS(target , function(err, headers){
if(err) return console.error('ERROR: ', err.message);
console.log(headers);
});
var https = require('https');
var url = require('url');
function getHeadersHTTPS(address, cb){
var protocol = url.parse(address).protocol;
if(protocol !== 'https:'){
return cb(new Error('address must be httpS'));
}
https.get(address)
.on('response', //... omitted ...
}
"takes callback" !== "automatically asynchronous"
// ...
if(protocol !== 'https:'){
return process.nextTick(function(){
cb(new Error('address must be httpS'));
}
}
// ...
Up Next: Node core APIs