Skip to content

[Refactor] Convert Promise chains to Async/Await to improve readability #54

@RythenGlyth

Description

@RythenGlyth

The project currently suffers from deep nesting due to extensive use of chained .then() and .catch() blocks. This increases cognitive load and makes the code difficult to maintain and debug.

We aim to modernize the codebase by adopting async/await syntax, which allows for a flatter structure and more readable control flow.

Guidelines & Constraints

  • Error Handling: Avoid wrapping large blocks of code in a single generic try/catch. Instead, use the "Hybrid Approach" by attaching .catch() directly to the awaitable promise when distinct error handling is required.

    • Bad: Wrapping 5 awaits in one try/catch where you lose context of which one failed.
    • Good:
      // Clear, flat error handling for specific steps
      const user = await getUser(id).catch(err => {
        throw new Error(`User fetch failed: ${err.message}`);
      });
      
      const config = await getConfig(user).catch(err => {
        console.warn('Config failed, using defaults');
        return DEFAULT_CONFIG; // possibly add a fallback here
      });
  • Parallelism: Be careful not to turn parallel operations into serial ones. If multiple promises were running concurrently (e.g., inside a Promise.all), keep them that way.

    • Bad: await a(); await b(); (if they don't depend on each other)
    • Good: await Promise.all([a(), b()]);

Acceptance Criteria

  • Nesting depth is significantly reduced.
  • Logic remains exactly the same (no functional changes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions