0

I am learning Node.js now and I have this question about async stuff.

Here is a code:

const http = require("http"),
    [_, _1, url1, url2, url3] = process.argv;

let strings = '';

const fetchData = async () => {
    const firstReq = await http.get(url1, data => {
        data.on('data', chunk => {
            strings += chunk
        })
    });

    const logRes = await test()
    return firstReq
}

const test = () => {
    console.log(` --- `, strings);
}

fetchData();

I was expecting that node will make GET request to url, add chunks to variable and then call next await step that will console.log resulted string with data.

Now code execution just prints empty string.

Why async/await does not work here? It is because http.get() does not return anything?

Thanks!

Nikita Shchypyplov
  • 1,090
  • 1
  • 9
  • 18

0 Answers0