私は、約束の結果を使って別のfectchリクエストを作成する必要があります。私はwordpressの残りのAPIを使って作業しています。そのカテゴリ名を取得するには、ポストを持つ2つの配列とカテゴリ名availablesを持つもう1つの配列を作成します。Javascript。入れ子になったフェッチの約束を待ちます
fetchAccordionData().then((data) =>
{
console.log(data.subCatList, data.posts);
for(let cat of data.subCatList)
{
console.log(cat);
}
});
ので、フェッチ秒の約束がそのように解決されたときにどのように私は知っていますか:ここに は私の関数は、今私はまだ準備ができてisn`t subCatList
配列関数を呼び出すとき
function fetchAccordionData()
{
const id = document.querySelector('.acc').getAttribute('data-id'),
wpRestAPI = '/wp-json/wp/v2/';
return fetch(wpRestAPI + 'posts?per_page=100&categories=' + id)
.then((resp) => resp.json())
.then((data) =>
{
let curId = [], subCatList = [];
data.map((post) =>
{
let catList = post.categories.filter((c) => c !== parseInt(id));
fetch(wpRestAPI + 'categories/' + catList[0])
.then((r) => r.json())
.then((cat) =>
{
if(!curId.includes(cat.id)) subCatList.push({id: cat.id, name: cat.name});
curId.push(cat.id);
});
});
return {'subCatList':subCatList, 'posts':data}
});
}
です私はデータを使うことができますか?
あなたは 'Promise.all()'を探しています。 – SLaks
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – Andreas