Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/bidi/Frame.ts (TypeScript)

Code before:
                fromEmitterEvent(navigation, 'fragment'),
                fromEmitterEvent(navigation, 'failed').pipe(
                  map(({url}) => {
                    throw new Error(`Navigation failed: ${url}`);
                  })
                ),
                fromEmitterEvent(navigation, 'aborted').pipe(
                  map(({url}) => {
                    throw new Error(`Navigation aborted: ${url}`);
                  })
                )
              ),
              switchMap(() => {
                if (navigation.request) {
                  function requestFinished$(
                    request: Request
                  ): Observable<Navigation> {
                    if (request.response || request.error) {
                      return of(navigation);
                    }
                    if (request.redirect) {
                      return requestFinished$(request.redirect);
                    }
                    // Reduces flakiness if the response events arrive after
                    // the load event.
                    // Usually, the response or error is already there at this point.
                    return fromEmitterEvent(request, 'success')
                      .pipe(
                        raceWith(fromEmitterEvent(request, 'error')),
                        raceWith(fromEmitterEvent(request, 'redirect'))
                      )
                      .pipe(
                        switchMap(() => {
                          return requestFinished$(request);
                        })
                      );
                  }
                  return requestFinished$(navigation.request);
                }
                return of(navigation);
              })
            );
          })
        ),
        this.#waitForNetworkIdle$(options),
      ]).pipe(
        map(([navigation]) => {
          const request = navigation.request;
          if (!request) {
            return null;
          }

Review comment (suggestion):
I think this comment is better suited for the `if (request.response || request.error) {` as here it implies that the `success`,`error` events most of the time are not emitted.

Code after:
                fromEmitterEvent(navigation, 'fragment'),
                fromEmitterEvent(navigation, 'failed').pipe(
                  map(({url}) => {
                    throw new Error(`Navigation failed: ${url}`);
                  })
                ),
                fromEmitterEvent(navigation, 'aborted').pipe(
                  map(({url}) => {
                    throw new Error(`Navigation aborted: ${url}`);
                  })
                )
              ),
              switchMap(() => {
                if (navigation.request) {
                  function requestFinished$(
                    request: Request
                  ): Observable<Navigation> {
                    // Reduces flakiness if the response events arrive after
                    // the load event.
                    // Usually, the response or error is already there at this point.
                    if (request.response || request.error) {
                      return of(navigation);
                    }
                    if (request.redirect) {
                      return requestFinished$(request.redirect);
                    }
                    return fromEmitterEvent(request, 'success')
                      .pipe(
                        raceWith(fromEmitterEvent(request, 'error')),
                        raceWith(fromEmitterEvent(request, 'redirect'))
                      )
                      .pipe(
                        switchMap(() => {
                          return requestFinished$(request);
                        })
                      );
                  }
                  return requestFinished$(navigation.request);
                }
                return of(navigation);
              })
            );
          })
        ),
        this.#waitForNetworkIdle$(options),
      ]).pipe(
        map(([navigation]) => {
          const request = navigation.request;
          if (!request) {
            return null;
          }