[Vuejs]-Vue js 2 โ€“ for loop in multiple rest calls fetchData

0๐Ÿ‘

I think you want to look at Promise.all. It will take an array of promises, wait for them all to complete, and then resolve with an array of results.

You would build your array of promises based on the array of slugs and ids in your first request. Maybe something like

const promises = result.data.articles.map((article) =>
    this.$http.get(`/cms/wp-json/wp/v2/cases-studies/?slug=${encodeURIComponent(article.slug)}`)
);

Getting the results is as easy as

Promise.all(promises).then((results) => {
    this.arrayOfSinglePages = results.map((result) => result.data);
});

Now your this.page has the array of id (and stuff) and this.arrayOfSinglePages has the page details for each of them in the same order.

๐Ÿ‘คRoy J

Leave a comment