异步函数的值或 HTTP 请求的值

Value of Async Function or Value of HTTP Request

晚上好,

我是 JavaScript 的新手。我想用 XMLHttpRequest 得到 innerhtml。主要代码有效,如果我只用 console.log 打印结果 - 我看到了我想要的,但我不知道如何将它保存为字符串,因为我需要使用它。你能帮帮我吗

我的代码:

function getBasketCode(){
    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = async function () {
        if (xhr.readyState == xhr.DONE) {
            //var code = xhr.responseText;
            var code = document.getElementById("shoppingcart_filled").innerHTML;
            console.log(code); // <-- here it shows me the right result
        }
    }
    xhr.open('GET', 'https://www.xxxxx.nl/xxxx', true);
    xhr.send();
}
getBasketCode();
function getBasketCode() {
  return new Promise((resolve) => {
    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = async function () {
      if (xhr.readyState == xhr.DONE) {
        var code = document.getElementById("shoppingcart_filled").innerHTML;
        resolve(code);
      }
    }
    xhr.open('GET', 'https://www.xxxxx.nl/xxxx', true);
    xhr.send();
  });
}
const code = await getBasketCode(); // assuming you are in an async function, otherwise getBasketCode().then((code) => {...