diff --git a/static/focus.js b/static/focus.js index 4b43735..767ebdb 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,37 @@ const API = { buhForms: "/api3/buh", }; -function run() { - sendRequest(API.organizationList, (orgOgrns) => { +async function run() { + try { + const orgOgrns = await sendRequest(API.organizationList); 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); - }); - }); - }); - }); + const requisitesPromise = sendRequest(`${API.orgReqs}?ogrn=${ogrns}`); + const analyticsPromise = sendRequest(`${API.analytics}?ogrn=${ogrns}`); + const buhPromise = sendRequest(`${API.buhForms}?ogrn=${ogrns}`); + const [requisites, analytics, buh] = await Promise.all([requisitesPromise, analyticsPromise, buhPromise]); + const orgsMap = reqsToMap(requisites); + addInOrgsMap(orgsMap, analytics, "analytics"); + addInOrgsMap(orgsMap, buh, "buhForms"); + render(orgsMap, orgOgrns); + } + catch (err){ + alert(err.message); + } } 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)); +function sendRequest(url) { + return fetch(url) + //.then(response => response) + .then(response => { + if (response.status >= 300){ + throw new Error(`status invalid - ${response.status}`); } - } - }; - - xhr.send(); + return response.json(); + }); + //.catch(reason => {}); + } function reqsToMap(requisites) {