Using Node.js, ubuntu, console.log does not appear during a Promise https.request, until, completion. How to flush or see in realtime?

43 views Asked by At

On server side Node.js I have a simple http.request ... this is in a simple script.js being run on the shell.

async function postRequest(_options, data) {
    return new Promise((resolve, reject) => {
        const req = https.request(_options, (res) => {
            let response = '';
            res.on('data', (chunk) => {
                response += chunk
            })

            res.on('end', () => {
                resolve(response);
            })
        })
        req.on('error', (err) => {
            reject(err)
        })
        req.write(data)
        req.end()
    })
}

called like

const resp = await postRequest(opt, JSON.stringify(req))

I simply added this

            res.on('data', (chunk) => {
                response += chunk
                console.log(">> ", response.length)
            })

Downloading 100s mb, hence, there are say 100 of the console logs over the course of a few minutes.

Unfortunately it doesn't work - the one hundred console logs are only spat out at the end, after completion of the promise.

How to simply see those messages in realtime?

I have tried using console.error, process.stdout.write, etc, but could be I've made some small mistake in those.

How to see the log messages in realtime as the Promise is proceeding?

Demo of the problem:

enter image description here

enter image description here

1

There are 1 answers

1
Bergi On BEST ANSWER

I cannot reproduce, your script works just fine.

The described behaviour is expected though when the server is processing the request for 2 minutes and only then begins responding. By logging the response (or its status code, headers etc) as soon as your receive it (and before starting to stream it), you can verify this.