Все ваши URL-адреса следуют одному и тому же шаблону, поэтому вы можете значительно сократить свой код, map
отправив массив ['', 'home', 'away', 'form']
на URL-адреса.Затем map
эти URL-адреса для Promises через makeHttpRequestTo
, а затем вы можете разбить ожидаемые результаты на свойства this.
:
async getStandings(seasonNr, matchdayNr) {
const urls = ['', 'home', 'away', 'form']
.map(str => `http://externService.com/${str}standings?seasonNr=${seasonNr}&matchdayNr=${matchdayNr}`);
const promiseArr = urls.map(makeHttpRequestTo);
[
this.standings,
this.homeStandings,
this.awayStandings,
this.formStandings
] = await Promise.all(promiseArr);
}
Чтобы заполнить каждое свойство по отдельности, а не ждать всех ответов навернись:
async getStandings(seasonNr, matchdayNr) {
['', 'home', 'away', 'form']
.forEach((str) => {
const url = `http://externService.com/${str}standings?seasonNr=${seasonNr}&matchdayNr=${matchdayNr}`;
makeHttpRequestTo(url)
.then((resp) => {
this[str + 'Standings'] = resp;
});
});
}