diff --git a/static/focus.js b/static/focus.js index 4b43735..72c99f9 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,38 +5,25 @@ const API = { buhForms: "/api3/buh", }; -function run() { - sendRequest(API.organizationList, (orgOgrns) => { - const ogrns = orgOgrns.join(","); - sendRequest(`${API.orgReqs}?ogrn=${ogrns}`, (requisites) => { - const orgsMap = reqsToMap(requisites); - sendRequest(`${API.analytics}?ogrn=${ogrns}`, (analytics) => { - addInOrgsMap(orgsMap, analytics, "analytics"); - sendRequest(`${API.buhForms}?ogrn=${ogrns}`, (buh) => { - addInOrgsMap(orgsMap, buh, "buhForms"); - render(orgsMap, orgOgrns); - }); - }); - }); - }); -} - -run(); - -function sendRequest(url, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("GET", url, true); - - xhr.onreadystatechange = function () { - if (xhr.readyState === XMLHttpRequest.DONE) { - if (xhr.status === 200) { - callback(JSON.parse(xhr.response)); - } - } - }; - - xhr.send(); -} +const sendRequest = async url => fetch(url) + .then(response => response.ok + ? response.json() + : alert('Ошибка HTTP: ' + response.status)); + +(async function run() { + const orgOgrns = await sendRequest(API.organizationList); + const ogrns = orgOgrns.join(","); + const [requisites, analytics, buh] = await Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?ogrn=${ogrns}`) + ]); + const orgsMap = reqsToMap(requisites); + + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buh, "buhForms"); + render(orgsMap, orgOgrns); +})(); function reqsToMap(requisites) { return requisites.reduce((acc, item) => { @@ -86,7 +73,7 @@ function renderOrganization(orgInfo, template, container) { orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] && orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] .endValue) || - 0 + 0 ); } else { money.textContent = "—";