File size: 3,450 Bytes
780c9fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | ---
title: AsyncDisposableStack.prototype.move()
short-title: move()
slug: Web/JavaScript/Reference/Global_Objects/AsyncDisposableStack/move
page-type: javascript-instance-method
browser-compat: javascript.builtins.AsyncDisposableStack.move
sidebar: jsref
---
The **`move()`** method of {{jsxref("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-nolint
move()
```
### Parameters
None.
### Return value
A new {{jsxref("AsyncDisposableStack")}} instance.
### Exceptions
- {{jsxref("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
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [JavaScript resource management](/en-US/docs/Web/JavaScript/Guide/Resource_management)
- {{jsxref("AsyncDisposableStack")}}
- {{jsxref("AsyncDisposableStack.prototype.disposeAsync()")}}
|