I have a module exporting a function who's goal is to collect all streamed string data from a URL and return the complete result:
'use strict'
const http = require("http");
const bl = require("bl");
async function getUrlData (url) {
http.get(url, (response) => {
response
.pipe(bl((err, data) => {
if(err) return console.log(err);
const stringData = data.toString();
return stringData
}));
});
}
module.exports = getUrlData;
I also have a main file which accepts 3 URLs as arguments and attempts to print out the value from getting the data from each URL in the exact order given.
'use strict'
const getData = require('./http-collect-module');
const urls = process.argv.slice(2);
const main = async () => {
urls.forEach((url) => {
var stringData = await getData(url);
console.log(stringData)
});
}
main();
I'm trying to make one get complete before moving onto the next, however am receiving this error:
var stringData = await getData(url);
^^^^^
SyntaxError: await is only valid in async function
I cannot understand why I'm getting this error since the main function is an async one. Any help appreciated