Is your feature request related to a problem? Please describe.
I am developing an ACME client module and I need to repeatedly make HTTPS requests, which I am currently doing using https.request(url[, options][, callback]). Due to the nature of an ACME client, I need to wait for a HTTPS request to finish before processing the data and then sending another request. I wait for the requests to complete using the await operator and by wrapping my code in an async function, as demonstrated in the example below:
(async() => {
let directory = await request(...);
...do some stuff...
let nonce = await request(...);
...do some stuff...
let account = await request(...);
...do some stuff...
)();
This becomes a nuisance, as I am unnecessarily making the code asynchronous just so that I can wait for HTTPS requests to finish in a synchronous manner. This becomes a further nuisance when users have to use the await operator themselves when using the ACME client module; this results in them having to use asynchronous code themselves and will lead to the module becoming unnecessarily complicated.
Describe the solution you'd like.
I would like to have a synchronous version of https.request(url[, options][, callback]) which takes the format of https.requestSync(url[, options]) and waits for the server to receive a full response before returning a value and continuing with the program. This will eliminate the need for the await operator and async functions, which will in turn enable functions in the ACME client module to be executed in a synchronous and uncomplicated manner, instead of the user having to chain functions.
There should also be synchronous versions of the following functions:
https.get(url[, options][, callback]).http.request(url[, options][, callback]).http.get(url[, options][, callback]).Describe alternatives you've considered.
There are a few solutions for synchronous requests on Stack Overflow: Synchronous request in Node.js (18 answers), but they all either provide external libraries, hacky solutions or overcomplicated code. I would prefer if Node.js had a simple, native synchronous version of https.request(url[, options][, callback]).
Would it be easier if there was a core function that sends HTTP request and returns a promise? Something similar to fetch and discussed here.
@shackijj I currently wrap the https.request(url[, options][, callback]) function in my own Promise in order to use the await keyword, so node-fetch won't really make the code any less complicated.
What I really want is to have a synchronous request() function, so that I can completely eliminate the need for async/await, so people using my code don't need to use async/await either, making the module I am developing much easier to use.
I don't think we will be adding this, partially for exactly the reason you want it. You are developing some sort of module, which performs some task. I, consuming your module, may be performing many tasks. If all those tasks are async, they can be in-flight at the same time. If one of those modules decides it needs to be sync for a "much easier to use" api, it actually slows me down.
Just to note: you could do a sync fetch now by using execSync(), spawning another node process or even curl to do the fetch.
-1 to adding sync network i/o
I think this is far enough away from the basic philosophy of Node.js to be relatively sure that this won鈥檛 be implemented, as the others above have explained. I鈥檒l close this, but thanks for the report anyway.
Just to note: you could do a sync fetch now by using execSync(), spawning another node process or even
curlto do the fetch.
Or you could use a Worker + Atomics.wait(), that probably has a bit less overhead and interacts less with the OS. But either solution still seems awkward when using await does the right thing here.
Most helpful comment
I don't think we will be adding this, partially for exactly the reason you want it. You are developing some sort of module, which performs some task. I, consuming your module, may be performing many tasks. If all those tasks are async, they can be in-flight at the same time. If one of those modules decides it needs to be sync for a "much easier to use" api, it actually slows me down.