AsyncDisposableStack.prototype.move()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The move() method of AsyncDisposableStack instances creates a new AsyncDisposableStack instance that contains the same disposers as this stack, and then marks this stack as disposed, without calling any disposers.

Syntax

js
move()

Parameters

None.

Return value

A new AsyncDisposableStack instance.

Exceptions

ReferenceError

Thrown if the stack is already disposed.

Examples

Claiming ownership of a stack

js
async function consumeStack(stack) {
  await using newStack = stack.move(); // newStack now owns the disposers
  console.log(stack.disposed); // true
  console.log(newStack.disposed); // false
  // newStack is disposed here immediately before the function exits
}

const stack = new AsyncDisposableStack();
console.log(stack.disposed); // false
await consumeStack(stack);
console.log(stack.disposed); // true

Allowing resources to be disposed within two code paths

The major use case of move() is when you have one or more resources which could either be disposed right here or could be persisted for later use. In this case, you can put the resources in an AsyncDisposableStack and then call move() when you need to persist the resources for later usage.

js
class PluginHost {
  #disposed = false;
  #disposables;
  #channel;
  #socket;

  static async init() {
    // Create an AsyncDisposableStack that is disposed when init exits.
    // If construction succeeds, we move everything out of `stack` and into
    // `#disposables` to be disposed later.
    await using stack = new AsyncDisposableStack();

    const channel = stack.use(await getChannel());

    const socket = stack.use(await getSocket());

    // If we made it here, then there were no errors during construction and
    // we can safely move the disposables out of `stack`.
    return new PluginHost(channel, socket, stack.move());

    // If construction failed, then `stack` would be disposed before reaching
    // the line above, which would dispose `channel` and `socket` in turn.
  }

  constructor(channel, socket, disposables) {
    this.#channel = channel;
    this.#socket = socket;
    this.#disposables = disposables;
  }

  [Symbol.asyncDispose]() {
    if (this.#disposed) {
      return;
    }
    this.#disposed = true;
    // Put `this.#disposables` into a `using` variable, so it is automatically
    // disposed when the function exits.
    await using disposables = this.#disposables;

    // NOTE: we can free `#socket` and `#channel` here since they will be
    // disposed by the call to `disposables[Symbol.asyncDispose]()`, below.
    // This isn't strictly a requirement for every disposable, but is
    // good housekeeping since these objects will no longer be useable.
    this.#socket = undefined;
    this.#channel = undefined;
    this.#disposables = undefined;
  }
}

Specifications

Specification
ECMAScript Async Explicit Resource Management
# sec-asyncdisposablestack.prototype.move

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
move
Chrome – Full support
Chrome 134 (Release date: 2025-03-04)
footnote Full support
Edge – Full support
Edge 134 (Release date: 2025-03-06)
footnote Full support
Firefox – Full support
Firefox 141 (Release date: 2025-07-22)
footnote Full support
Opera – Full support
Opera 119 (Release date: 2025-05-13)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 134 (Release date: 2025-03-04)
footnote Full support
Firefox for Android – Full support
Firefox for Android 141 (Release date: 2025-07-22)
footnote Full support
Opera Android – Full support
Opera Android 88 (Release date: 2025-03-19)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 29 (Release date: 2025-10-25)
footnote Full support
WebView Android – Full support
WebView Android 134 (Release date: 2025-03-04)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Bun – Full support
Bun 1.3 (Release date: 2025-10-10)
footnote Full support
Deno – Full support
Deno 2.2.10 (Release date: 2025-04-15)
footnote Full support
Node.js – Full support
Node.js 24 (Release date: 2025-05-06)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also