Synchronization 如何在JavaScript中同步抓取到其他抓取
目的 我想从第一次获取GetComPaniesData()获取数据。然后我必须使用第一个fetch数据的id从第二个fetch getIncomesData(id)获取每个数据。我想在两次回迁都获得完整数据并结束后记录数据 功能 获取数据Synchronization 如何在JavaScript中同步抓取到其他抓取,synchronization,fetch,Synchronization,Fetch,目的 我想从第一次获取GetComPaniesData()获取数据。然后我必须使用第一个fetch数据的id从第二个fetch getIncomesData(id)获取每个数据。我想在两次回迁都获得完整数据并结束后记录数据 功能 获取数据 const getCompaniesData = async () => { try { const response = await fetch(baseUrl); const data = await resp
const getCompaniesData = async () => {
try {
const response = await fetch(baseUrl);
const data = await response.json();
return data;
} catch (error) {
console.log(error);
}
};
const getIncomesData = async (id) => {
try {
const response = await fetch(incomesUrl+id);
const data = await response.json();
return data;
} catch (error) {
console.log(error);
}
};
window.onload = () => {
const companiesTab = [];
const incomesTab = [];
getCompaniesData()
.then(compData => {
compData.map(item => {
let company = new Company(item.id, item.name, item.city, 0);
companiesTab.push(company);
});
companiesTab.map( company => {
//How to fetch here always full data from fetch getIncomesData(id)?
getIncomesData(company.id).then(incomData => {
let incomes = new Incomes(incomData.id, incomData.incomes);
incomes.sumIncomes(incomes);
incomesTab.push(incomes);
company.tIncomes = (Math.round(incomes.sum * 100) / 100).toFixed(2);
});
});
})
.then( () => {
//How to have full data here (It's 300 cells in tab)?
console.log(companiesTab);
console.log(incomesTab);
});
}