#js array and promises different output in return an console.log()
9 messages · Page 1 of 1 (latest)
Await a
Well actually, await scrpData on the return line
I dunno if you can await on the top level...
Just a sec gonna check
You can't await at the top level and since a is async the top level immediately goes to the log before ever running a. Two options.
- Use a second async function that does all the top level stuff so you can await.
async function a() {
getHtml = await fetch("https://www.npmjs.com/");
htmlText = await getHtml.text();
return htmlText.substring(0, 20);
}
async function run() {
dataList = await a();
console.log(dataList);
}
run();
- Use
thenwith a function (arrow function in this case)
async function a() {
getHtml = await fetch("https://www.npmjs.com/");
htmlText = await getHtml.text();
return htmlText.substring(0, 20);
}
a().then(dataList => console.log(dataList));
I'd suggest the first option.