refactor: stop awaiting promises one by one in loops - #1545
Merged
Merged
Conversation
Fixes SonarCloud typescript:S9382 (await inside a loop) and typescript:S7503 (async function without await) across the frontend and backend. Loops that must stay ordered now go through small helpers in utils/sequential.ts (forEachSequential, mapSequential, firstResultSequential, forEachPage; chunk and forEachConcurrent on the frontend). Independent work uses Promise.all or a bounded pool. Async functions without await drop async and return an explicit promise, or keep async with return await where a synchronous throw must still reject.
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Repository: bobbyquantum/inkweld/.coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The frontend and backend each carried an identical copy of the sequential promise helpers, which SonarCloud reports as duplication. Move them into a shared workspace package, wired up like @inkweld/presence (tsconfig path aliases, package tests in CI, coverage for SonarCloud), and fold the two Fal.ai model listings into one category-parameterised function.
Merge the duplicate @inkweld/async import, write firstResultSequential's guard as an early return, and build the EPUB worldbuilding sections from one array literal instead of repeated pushes.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
SonarCloud added
typescript:S9382(await inside a loop) and reportstypescript:S7503(async function withoutawait). Existing code in touched files trips both and fails the quality gate on PRs. This clears them acrossfrontend/src/appandbackend/src(generatedapi-clientexcluded), without disabling any rule.An AST sweep of the tree finds 0 remaining hits for either pattern (141 loops and 106 async functions before).
How loops were handled
New helpers, with unit tests (
frontend/src/app/utils/sequential.ts,backend/src/utils/sequential.ts):forEachSequential,mapSequential,firstResultSequential,forEachPage(cursor pagination), pluschunkandforEachConcurrenton the frontend. They express "one at a time" as an explicit promise chain, and each call site says in a comment why it is sequential.mapWithConcurrency(backend) now recurses instead of looping.Became concurrent (independent items, order of results preserved):
getElementImageUrls(reference image lookups),FileStorageServicedirectory size /listProjectFilesstats /deleteSlotImage, account-deletion profile image cleanups, MCP worldbuilding search reads and tombstone lookups (both bounded to 8 viamapWithConcurrency).verifyLocalAvailability, delete-project-databases in the home page and project settings.forEachConcurrent/mapWithConcurrency.Kept sequential: IndexedDB work on one database (storage-context clone/rekey/rename/clear, migration, snapshots, project cache), Yjs document opens, ordered import/export/publish/EPUB/PDF/HTML/Markdown generation (chapter numbers, shared image caches, filenames assigned in order), progress reporting per item, remote-store sync (Dropbox/Nextcloud, rate limited), server calls with per-item error handling, batches (
chunkthen sequential batches, each batch still concurrent), DO/D1/R2 batched writes, activity event writes, and everything with early exit (firstResultSequential) or cancellation. Cursor pagination, polling, stream reading, retry loops and the interactive password prompt are inherently sequential and are now recursive/forEachPage.The reset page keeps its database deletions sequential (its spec mocks a single shared delete request).
S7503
asyncdropped where nothing awaits, returningPromise.resolve/rejectfor plain values. Where the body can throw synchronously and callers rely on a rejection (a handful of functions),asyncis kept and the returned promise isreturn awaited, so behaviour is unchanged.AGENTS.mdhas a short note on the patterns.Testing
npm run test:coverage394 files / 9420 tests pass, coverage above thresholds;npm run lintclean.bun test1797 pass;eslint srcno errors (existing warnings only);tscshows only the errors already present on main.isolate: false, runningstorage-context.service.spec.tsin the same worker after certain page specs can time out on main as well (reproduced on an unmodified checkout); the full suite is green.