Repository: microsoft/playwright
File: packages/playwright/src/reporters/multiplexer.ts (TypeScript)

Code before:
    for (const reporter of this._reporters)
      wrap(() => reporter.onBegin?.(suite));
  }

  onTestBegin(test: TestCase, result: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onTestBegin?.(test, result));
  }

  onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onStdOut?.(chunk, test, result));
  }

  onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onStdErr?.(chunk, test, result));
  }

  async onTestPaused(test: TestCase, result: TestResult) {
    const promises: Promise<void>[] = [];
    for (const reporter of this._reporters) {
      if (reporter.onTestPaused)
        promises.push(wrapAsync(() => reporter.onTestPaused!(test, result)));
    }
    // first reporter that resolves continues the execution.
    await Promise.race(promises);
  }

  onTestEnd(test: TestCase, result: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onTestEnd?.(test, result));
  }

  async onEnd(result: FullResult) {
    for (const reporter of this._reporters) {
      const outResult = await wrapAsync(() => reporter.onEnd?.(result));
      if (outResult?.status)
        result.status = outResult.status;
    }
    return result;
  }

  async onExit() {
    for (const reporter of this._reporters)
      await wrapAsync(() => reporter.onExit?.());
  }

  onError(error: TestError) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onError?.(error));

Review comment (suggestion):
I find `await new Promise<void>(() => {});` to be surprising. Why not the other way around? Whoever wants to hold the execution, resolves later, and we `Promise.all()` them. We can even call them sequentially, like `onEnd()`.

Code after:
    for (const reporter of this._reporters)
      wrap(() => reporter.onBegin?.(suite));
  }

  onTestBegin(test: TestCase, result: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onTestBegin?.(test, result));
  }

  onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onStdOut?.(chunk, test, result));
  }

  onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onStdErr?.(chunk, test, result));
  }

  async onTestPaused(test: TestCase, result: TestResult) {
    for (const reporter of this._reporters)
      await wrapAsync(() => reporter.onTestPaused?.(test, result));
  }

  onTestEnd(test: TestCase, result: TestResult) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onTestEnd?.(test, result));
  }

  async onEnd(result: FullResult) {
    for (const reporter of this._reporters) {
      const outResult = await wrapAsync(() => reporter.onEnd?.(result));
      if (outResult?.status)
        result.status = outResult.status;
    }
    return result;
  }

  async onExit() {
    for (const reporter of this._reporters)
      await wrapAsync(() => reporter.onExit?.());
  }

  onError(error: TestError) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onError?.(error));
  }

  onStepBegin(test: TestCase, result: TestResult, step: TestStep) {
    for (const reporter of this._reporters)
      wrap(() => reporter.onStepBegin?.(test, result, step));