大网数据广告活动
Web 标准与前端开发

AsyncDisposableStack.prototype.disposeAsync()

· 52CE 我爱测

The **`disposeAsync()`** method of AsyncDisposableStack instances disposes this stack by calling all disposers registered to it in reverse order of registration, awaiting for each one's completion before calling the next one. If the stack is already disposed, this method does not

The disposeAsync() method of AsyncDisposableStack instances disposes this stack by calling all disposers registered to it in reverse order of registration, awaiting for each one's completion before calling the next one. If the stack is already disposed, this method does nothing.

It performs the same action as await using disposer = new AsyncDisposableStack() at scope exit. It can be used if you need to clean up at a point other than scope exit.

Syntax

disposeAsync()

Parameters

None.

Return value

A new Promise that resolves with undefined when all registered disposers have completed in sequence.

Exceptions

disposeAsync() never synchronously throws an error. The returned promise may reject with one of the following errors:

  • SuppressedError
    • : Thrown if multiple disposers in the stack threw an error. If only one error is thrown, it is rethrown as-is. Otherwise, for each additional error, a new SuppressedError is created, with the original error as the suppressed property, and the new error as the error property.

Examples

Disposing a stack

Here we push three disposers to the stack, using the AsyncDisposableStack/use, AsyncDisposableStack/adopt, and AsyncDisposableStack/defer methods. When disposeAsync() is called, the disposers are called in reverse order of registration.

Note that usually you don't need to call disposeAsync() manually. Declare the stack with Statements/await_using, and its [Symbol.asyncDispose]() method will be automatically called when the stack goes out of scope.

class Resource {
  #doDisposal() {
    // Imagine more meaningful disposal logic here
    return new Promise((resolve) => {
      setTimeout(resolve, 1000);
    });
  }
  async dispose() {
    await this.#doDisposal();
    console.log("Resource disposed");
  }
  async [Symbol.asyncDispose]() {
    await this.#doDisposal();
    console.log("Resource disposed via Symbol.asyncDispose");
  }
}

async function doSomething() {
  const disposer = new AsyncDisposableStack();
  const resource = disposer.use(new Resource());
  const resource2 = disposer.adopt(new Resource(), (resource) =>
    resource.dispose(),
  );
  disposer.defer(() => console.log("Deferred disposer"));
  disposer.disposeAsync();
  // Logs in order:
  // Deferred disposer
  // Resource disposed
  // Resource disposed via Symbol.dispose
}

doSomething();

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。