为什么 XMLHttpRequestEventTarget.onerror 在同步请求中不起作用?

why XMLHttpRequestEventTarget.onerror doesn't work in synchronous request?

我尝试了 XMLHttpRequestEventTarget.onerror 来处理请求失败的情况,但它只适用于异步请求。虽然我确实需要一个同步请求,因为它使代码对我来说更容易并且按顺序工作。 代码

const http = new XMLHttpRequest();
const url = 'http://192.168.1.2/';
http.open('get', url, false ); 



  http.onreadystatechange = function ()
    {
        if(http.readyState === 4)
        {
            if(http.status === 200)
            {

console.log("it worked ")

            }

        }
    }

   http.onerror = function () {
  console.log("** An error occurred during the transaction");
  };


http.send();

enter image description here

只需将其转回 async(true) 并使用回调修复其余代码!