title: JS Promise.all in recursion
body: <p>I need some help with JS promises.</p>
  <p>There is a data on a server:</p>
  <p>{ id: 1,
  name: 'org_1',
  parent_id: 0
  },
  { id: 2,
  name: 'org_2',
  parent_id: 0
  },
  { id: 3,
  name: 'dep_1',
  parent_id: 1
  },
  { id: 4,
  name: 'dep_2',
  parent_id: 1
  },
  { id: 5,
  name: 'dep_1',
  parent_id: 2
  }
  ...</p>
  <p>And a simple peace of code getting data:</p>
  <pre><code>const fetchData = async (params) =&gt; {
    const url = new URL(REMOTE_APP_HOST);
    url.search = new URLSearchParams(params).toString();
  
    const response = await fetch(url, {
      credentials: &quot;include&quot;,
      headers: new Headers({
        Authorization: &quot;Basic &quot; + API_ACCOUNT,
        &quot;Content-Type&quot;: &quot;application/json&quot;,
      }),
    });
    return await response.json();
  };
  
  const findOrgs = async (parent_id) =&gt; {
    const params = {
      parent_id: parent_id,
    };
  
    try {
      return await fetchData(params);
    } catch (err) {
      console.log(&quot;findOrgs, fetch error: &quot;, err);
    }
  };
  
  const orgsData = async (parent_id) =&gt; {
    const data = []
   
    const organizations = await findOrgs(parent_id);
  
    for (const organization of organizations) {
      const organizationData = {
        name: organization.name,
        type: &quot;organization&quot;,
        children: [],
      };
  
      organizationData.children = await orgsData(
        organization.id
      );
  
      data.push(organizationData);
    }
  
    return data
  };
  
  const orgsList = async () =&gt; {
    return await orgsData(0);
  };
  </code></pre>
  <p>How to implement a parallel version of the 'orgsData' function using Promise.all?</p>
  <p>My effort:</p>
  <pre><code>const organizationData = async (organization) =&gt; {
    const data = [];
  
    const orgData = {
      name: organization.name,
      type: &quot;organization&quot;,
      children: [],
    };
  
    const subOrgs = await orgsData_2(organization.id);
    orgData.children = subOrgs.map(organizationData);
  
    data.push(orgData);
  
    return data;
  }
  
  const orgsData_2 = async (parent_id) =&gt; {
    
    const organizations = await findOrganizations(parent_id);
  
    const promises = organizations.map(organizationData);
  
    return await Promise.all(promises);
  }
  </code></pre>
  <p>but it's not working....</p>