asnyc await javascript get request code example

Example 1: node js request async await

function doRequest(url) {
  return new Promise(function (resolve, reject) {
    request(url, function (error, res, body) {
      if (!error && res.statusCode == 200) {
        resolve(body);
      } else {
        reject(error);
      }
    });
  });
}

// Usage:

async function main() {
  let res = await doRequest(url);
  console.log(res);
}

main();

Example 2: javascript async fetch file html

async function () {
element.innerHTML = await (await fetch('./filetofetch.html')).text();
}