From cfc7d2ea482c734a5f15068b9cef57e89fb0d62b Mon Sep 17 00:00:00 2001 From: ihatememore Date: Wed, 16 Jun 2021 21:17:44 +0500 Subject: [PATCH] 1-4 --- static/focus.js | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/static/focus.js b/static/focus.js index 4b43735..7196b4c 100644 --- a/static/focus.js +++ b/static/focus.js @@ -5,37 +5,34 @@ const API = { buhForms: "/api3/buh", }; -function run() { - sendRequest(API.organizationList, (orgOgrns) => { - const ogrns = orgOgrns.join(","); - sendRequest(`${API.orgReqs}?ogrn=${ogrns}`, (requisites) => { +async function run() { + let orgOgrns = await sendRequest(API.organizationList) + let ogrns = orgOgrns.join(","); + + Promise.all([ + sendRequest(`${API.orgReqs}?ogrn=${ogrns}`), + sendRequest(`${API.analytics}?ogrn=${ogrns}`), + sendRequest(`${API.buhForms}?ogrn=${ogrns}`) + ]) + .then(([requisites, analytics, buh]) => { 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); - }); - }); - }); - }); + addInOrgsMap(orgsMap, buh, "buhForms"); + addInOrgsMap(orgsMap, analytics, "analytics"); + render(orgsMap, orgOgrns); + }) + .catch(error => alert(error)) } 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(); +async function sendRequest(url) { + let fetchURL = await fetch(url); + if (fetchURL.ok) { + return await fetchURL.json(); + } + else { + throw new Error("Ошибка HTTP: " + fetchURL.status) + } } function reqsToMap(requisites) {