DEV Community

Russell R.
Russell R.

Posted on

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

I was just wondering if the NodeJS https.get() method/function was asynchronous or not?

I'm still learning, but it seems like it is, since it was producing results before other code of mine, but I could be wrong.

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

This is what I'm talking about:
https://nodejs.org/api/https.html#https_https_get_url_options_callback

Top comments (1)

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!