Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .coderabbit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reviews:
tools:
# oxlint does not run if biome is enabled
biome:
enabled: false
14 changes: 14 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"rules": {
"typescript": "error",
"correctness": "error",
"suspicious": "error",
"perf": "warn",
"style": "warn"
},
"env": {
"browser": true,
"es2021": true,
"node": true
}
}
16 changes: 16 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function greet(name: string) {
console.log("Hello, " + name)
}

let unused = 123

const x = 1
x = 2

const items = [1, 2, 3]
items.forEach(async item => {
await fetch(`/api/${item}`)
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Replace forEach with for-of loop for async operations.

Using async callbacks with forEach doesn't work as expected since forEach doesn't await the promises. The async callbacks will run concurrently without proper sequencing.

-items.forEach(async item => {
-  await fetch(`/api/${item}`)
-})
+for (const item of items) {
+  await fetch(`/api/${item}`)
+}

Alternatively, if concurrent execution is desired:

-items.forEach(async item => {
-  await fetch(`/api/${item}`)
-})
+await Promise.all(items.map(async item => {
+  await fetch(`/api/${item}`)
+}))
🤖 Prompt for AI Agents
In test.ts around lines 11 to 13, the use of forEach with an async callback does
not properly await the asynchronous operations, causing them to run concurrently
without sequencing. Replace the forEach loop with a for-of loop to properly
await each fetch call sequentially. This ensures that each asynchronous
operation completes before moving to the next iteration.


greet("world")