Library › Coding AI › Software developers
Fix an async forEach without breaking the rate limit
Practice: Giving the right context. About 5 minutes.
About this exercise
syncAll returns before any upload finishes because of forEach with an async callback. Fix it so it awaits each upload in order with for...of. The partner API allows 4 requests a second, so it must stay sequential (no Promise.all) and await the existing sleep(250) after each upload. Keep the name, parameter and results array. Ops also wants failures logged with console.error(`sync: upload failed for ${item.id}`, err) instead of console.warn. Return only syncAll in exactly 1 code block of at most 16 lines, under 20 words of text.
Common mistakes when you ask AI for this
- Going parallel. The usual fix is Promise.all, which fires 37 requests at once and gets the key paused. Try: Add: "Keep it sequential with for...of; no Promise.all."
- Ignoring the rate limit. The AI can't know about the limit or the unused sleep helper unless you point at them. Try: Add: "await sleep(250) after each upload."
- Missing the new log line. The AI keeps console.warn; it can't know ops wants console.error with a sync: prefix. Try: Add: "In the catch: console.error(`sync: upload failed for ${item.id}`, err)."
- The whole file and a lesson. The AI reprints the module and explains why forEach ignores promises. Try: Add: "Only syncAll, one code block, no text."
What a strong prompt includes
- Asks for sequential calls
- Mentions the rate limit
- Changes the log line
- Asks for only the function
- Limits the reply
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)