Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

351
Vistas
¿Cómo refactorizar bucle for async/await con Promise.all()?

Estoy tratando de entender cómo usar Promise.all() en este código. He leído en artículos que puede ejecutar operaciones asíncronas en paralelo con Promise.all() para optimizar la velocidad. Aquí está el código actual en bucles for anidados (malo):

 type ListGroup = { listId: string groupIds: Array<string> } const listsAndGroups: Array<ListGroup> = []; // <-- put everything here const { lists } = await mailchimp.get('/lists'); for (const list of lists) { const listObj = { listId: list.id }; const { categories } = await mailchimp.get( `/lists/${list.id}/interest-categories`, ); for (const category of categories) { const { interests } = await mailchimp.get( `/lists/${list.id}/interest-categories/${category.id}/interests`, ); Object.defineProperty(listObj, 'groupIds', { value: interests.map((interest) => interest.id), enumerable: true, }); } listsAndGroups.push(listObj); }

Así es como lo estoy haciendo hasta ahora, creo que solo estoy corriendo a ciegas aquí sin saber realmente lo que estoy haciendo:

 const listsAndGroups: Array<ListGroup> = await getListsGroups(); // <-- put everything here const getListsGroups = async () => { const { lists } = await mailchimp.get('/lists'); const listGroups = lists.map((list) => getCategories(list.id).then((groups) => groups.map((group: Record<'groupIds', string>) => { return { listId: list.id, ...group, }; }), ), ); return Promise.all(listGroups); }; const getCategories = async (listId: string) => { const { categories } = await mailchimp.get( `/lists/${listId}/interest-categories`, ); const groups = categories.map((category) => getInterests(listId, category.id), ); return Promise.all(groups); }; const getInterests = async (listId: string, categoryId: string) => { const { interests } = await mailchimp.get( `/lists/${listId}/interest-categories/${categoryId}/interests`, ); return { groupIds: interests.map((interest) => interest.id) }; };
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Podría simplificar su operación de muchas maneras, aquí hay una:

 type ListGroup = { listId: string groupIds: Array<string> } const listsAndGroups: Array<ListGroup> = []; // <-- put everything here const { lists } = await mailchimp.get('/lists'); const pandingLists = lists.map(list => mailchimp.get(`/lists/${list.id}/interest-categories`) .then(data => [data, { listId: list.id }]) ); for (const [{ categories }, listObj] of await Promise.all(pandingLists)) { const batch = categories.map(({ id }) => mailchimp.get(`/lists/${listObj.listId}/interest-categories/${id}/interests`).then(interests => { Object.defineProperty(listObj, 'groupIds', { value: interests.map(({ id }) => id), enumerable: true, }); })); await Promise.all(batch).then(() => listsAndGroups.push(listObj)); }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda