async function ajax({ method = "GET", url = ".", body = undefined, headers = {}, on_success = () => {}, on_failure = () => {}, }) { const xhttp = new XMLHttpRequest(); const return_on_sent = new Promise((resolve, reject) => { try { xhttp.onreadystatechange = () => { if (xhttp.readyState == 4) { const response_status = String(xhttp.status); if (/2\d\d/.test(response_status)) { resolve(on_success(xhttp.responseText, xhttp)); } else { resolve(on_failure(xhttp.responseText, xhttp)); } } }; } catch (err) { reject(err); } }); xhttp.open(method, url, true); Object.keys(headers).forEach((header_key) => { xhttp.setRequestHeader(header_key, headers[header_key]); }); xhttp.send(body); return return_on_sent; }