docs.rodeo

MDN Web Docs mirror

Symbol.dispose

The Symbol.dispose static data property represents the well-known symbol Symbol.dispose. The {{jsxref("Statements/using", "using")}}  declaration looks up this symbol on the variable initializer for the method to call when the variable goes out of scope.

Value

The well-known symbol Symbol.dispose.

{{js_property_attributes(0, 0, 0)}} 

Description

An object is disposable if it has the [Symbol.dispose]() method. The method is expected to have the following semantics:

This method should not return a promise, as promises returned by [Symbol.dispose]() are not awaited by {{jsxref("Statements/await_using", "await using")}} . To declare async disposables, use {{jsxref("Symbol.asyncDispose")}} .

Examples

User defined disposables

[Symbol.dispose] allows the creation of custom disposables. See the {{jsxref("Statements/using", "using")}}  reference for more information.

class Disposable {
  constructor() {
    this.disposed = false;
  }

  [Symbol.dispose]() {
    this.disposed = true;
  }

  get isDisposed() {
    return this.disposed;
  }
}

const resource = new Disposable();
{
  using resourceUsed = resource;
  console.log(resource.isDisposed); // false
}
console.log(resource.isDisposed); // true

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN