Как дождаться подгрузки всех промисов, а затем отренедерить их?
Есть функция рендера страницы со списком данных о фильме:
export function render(data) {
const planets = data.planets;
const species = data.species;
const starships = data.starships;
const container = document.createElement('div');
container.classList.add('container', 'pt-4');
const filmTitle = document.createElement('h1');
const btnBack = document.createElement('button');
const filmDescription = document.createElement('p');
const filmListPlanets = document.createElement('ul');
const filmListTitlePlanets = document.createElement('h2');
const filmListSpecies = document.createElement('ul');
const filmListTitleSpecies = document.createElement('h2')
const filmListStarships = document.createElement('ul');
const filmListTitleStarships = document.createElement('h2');
filmListPlanets.classList.add('list-group');
filmListSpecies.classList.add('list-group');
filmListStarships.classList.add('list-group');
btnBack.classList.add('btn', 'btn-primary');
btnBack.textContent = 'Back to episodes';
btnBack.addEventListener('click', () => {
location = '/index.html';
})
filmTitle.textContent = `${data.title} ${data.episode_id}`;
filmDescription.textContent = data.opening_crawl;
filmListTitlePlanets.textContent = 'Planets';
filmListTitleSpecies.textContent = 'Species';
filmListTitleStarships.textContent = 'Starships';
Promise.all([
getDetails(planets),
getDetails(species),
getDetails(starships)
])
.then(res => {
renderDetailsPage2(res[0], filmListPlanets)
renderDetailsPage2(res[1], filmListSpecies)
renderDetailsPage2(res[2], filmListStarships)
container.append(
btnBack,
filmTitle,
filmDescription,
filmListTitlePlanets,
filmListPlanets,
filmListTitleSpecies,
filmListSpecies,
filmListTitleStarships,
filmListStarships,
)
})
return container;
}
Функция getDetails получает массив с url api и выдает промис:
export function getDetails(apiUrl) {
return new Promise(resolve => {
const data = apiUrl.map(url => fetch(url)
.then(res => res.json()))
resolve(data);
})
}
Функция renderDetailsPage2:
export function renderDetailsPage2(dataDetails, filmList) {
return dataDetails.map(detail => {
detail.then(res => {
const filmItem = document.createElement('li');
filmItem.classList.add('list-group-item', 'd-flex', 'flex-column');
filmItem.textContent = res.name;
filmList.append(filmItem);
})
})
}
В Promise.all я пытаюсь дождаться подгрузки промисов, и затем отрендерить их одновременно со всей страницей. Но страница рендериться последовательно. Сначала основной контент, а затем данные в списках.
Как сделать так, чтобы страница показывалась в тот момент, когда все необходимые данные будут подгружены?