To fix await is not working for Node request module, we switch to the node-fetch
module.
To use it, we write
const url = "http://www.example.com";
try {
const response = await fetch(url);
const json = await response.json();
return { message: json.message, status: json.type };
} catch (error) {
console.log(error);
}
to call fetch
with the url
to make a get request to it.
It returns a promise so we can use await
to get the response
.
And then we use await
again to get the response body returned by the promise returned by response.json
.