Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright ManualPromise

From Leeroopedia

Template:Implementation Page

Overview

ManualPromise extends the native Promise class with externally accessible resolve() and reject() methods, providing a deferred pattern for async coordination throughout Playwright.

Description

The module provides two classes:

  • ManualPromise<T> -- a Promise subclass that exposes resolve() and reject() as instance methods, along with an isDone() check. It uses Symbol.species to ensure derived promises (from .then()) are standard Promises.
  • LongStandingScope -- manages a collection of races (ManualPromises) that can all be terminated simultaneously, useful for managing cancellation of multiple concurrent operations tied to a single lifecycle scope.

Usage

Used extensively throughout Playwright for async coordination, progress tracking, and lifecycle management where the resolve/reject decision is external to the promise creation.

Code Reference

Source Location

packages/playwright-core/src/utils/isomorphic/manualPromise.ts (122 lines)

Class Signatures

export class ManualPromise<T = void> extends Promise<T> {
  constructor()
  isDone(): boolean
  resolve(t: T): void
  reject(e: Error): void
}

export class LongStandingScope {
  isClosed(): boolean
  close(error: Error): void
  race<T>(promise: Promise<T>): Promise<T>
}

Import

import { ManualPromise, LongStandingScope } from '../utils/isomorphic/manualPromise';

I/O Contract

ManualPromise

  • Created without arguments
  • resolve(value) and reject(error) can be called externally
  • isDone() returns true after resolve or reject

LongStandingScope

  • race(promise) -- wraps a promise that will be rejected if the scope is closed
  • close(error) -- terminates all pending races with the given error

Usage Examples

Basic Usage

const promise = new ManualPromise<string>();
// ... later, from another context:
promise.resolve('done');
const result = await promise; // 'done'

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment