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) }; };
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)); }