Guest User

Untitled

a guest
Mar 19th, 2021
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. async function fetchRecursive(base_url, arr, level) {
  2. const result = [];
  3. if(level == 0) return result;
  4. if (Array.isArray(arr))
  5. for (const n of arr) {
  6. if (n.children) {
  7. let ch_arr;
  8. try {
  9. result.push({
  10. id: n.id,
  11. externalId: n.externalId,
  12. text: n.text,
  13. children: await fetch(base_url + n.id).then((r) => {
  14. ch_arr = { ...r };
  15. return r.json();
  16. }),
  17. productCount: n.productCount,
  18. });
  19. } catch (error) {
  20. result.push({ id: n.id, error });
  21. }
  22. result.push(...(await fetchRecursive(base_url, ch_arr, level - 1)));
  23. } else {
  24. result.push(n);
  25. }
  26. }
  27. return result;
  28. }
  29. данные такого вида
  30. {id: 13661, externalId: "КЗ000000000000002853", text: "J-Y (ST) Y", children: false, productCount: 3}
Advertisement
Add Comment
Please, Sign In to add comment