|

Day 53 of 100: Typed Async — Promise, Awaited, and the Retry Helper

Day 53 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


Day 24 gave you async/await. Today: full typing of async flows — the exact patterns in Playwright suites.

Contents

Typed Async Flows

type User = { id: number; name: string };

async function createUser(name: string): Promise {
  return { id: Date.now(), name };
}

// Awaited unwraps promise types:
type CreateResult = Awaited>;   // User

// Typed parallel setup:
const [admin, viewer]: [User, User] = await Promise.all([
  createUser('admin'),
  createUser('viewer'),
]);

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

Typed Retry Helper

async function withRetry(
  fn: () => Promise,
  attempts = 3,
  delayMs = 500,
): Promise {
  let lastError: unknown;
  for (let i = 1; i <= attempts; i++) {
    try { return await fn(); }
    catch (e) {
      lastError = e;
      if (i < attempts) await new Promise(r => setTimeout(r, delayMs));
    }
  }
  throw lastError instanceof Error ? lastError : new Error(String(lastError));
}

const user = await withRetry(() => createUser('flaky-env'));   // user: User

Floating Promise Defense

// ESLint rule that catches missing await:
// '@typescript-eslint/no-floating-promises': 'error'
// createUser('x');         ← flagged: promise ignored
// await createUser('x');   ← clean
// THE single most valuable lint rule for Playwright projects.

Tomorrow (Day 54): Typed error handling — unknown catches and error unions.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.