• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

274
Views
¿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) }; };
about 3 years ago · Santiago Trujillo
1 answers
Answer question

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)); }
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error