DEV Community

Discussion on: Is NodeJS's https.get function/method asyncronous?

Collapse
 
rhymes profile image
rhymes

Hi Russell, I did a quick test taking the example in nodejs.org/api/https.html#https_ht... and adding something like:

console.log("before");
// ... 
console.log("after");

and it printed "after" before the callback but it could have printed in the correct order, that's the nature of async. You can't expect the callback to finish before the code after it.

I'm not surprised as most things are async in Node.

Also, is https.get technically a function, or method? I wasn't sure when writing the title.

It's both:

const https = require('https');
console.log(typeof https.get);
> function

but also get is a function operating on (better said "scoped to") the https object, therefore it's a method of the object, but it's also a function.

In this case get is a method of the object https which accepts a URL and a function as arguments.

Hope this helps!