Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function fetchRecursive(base_url, arr) {
- const result = [];
- const promises = [];
- if (Array.isArray(arr))
- for (const n of arr) {
- if (n.children) {
- let ch_arr;
- try {
- const data = {
- id: n.id,
- externalId: n.externalId,
- text: n.text,
- children: fetch(base_url + n.id).then((r) => {
- ch_arr = { ...r };
- return r.json();
- }),
- productCount: n.productCount,
- };
- data.children.then(children => data.children = children);
- promises.push(data.children);
- result.push(data);
- } catch (error) {
- result.push({ id: n.id, error });
- }
- result.push(...(await fetchRecursive(base_url, ch_arr)));
- } else {
- result.push(n);
- }
- }
- await Promise.all(promises);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment