These features come from proposals that reached stage 4 after the 17th edition of ECMA-262. The text of each is in the ES draft: explicit resource management (clauses 7.5, 14.3.1, 14.7.5, 27.4, 27.5, 20.5), iterator chunking and sequencing helpers (27.1.3), joint iteration (27.1.2.1 Iterator.zip, Iterator.zipKeyed) and Atomics.pause (25.4).
using and await using
using x = expr;andawait using x = expr;declare a block-scoped constant (the binding behaves asconst) and register the value for disposal when the scope exits.await usingneeds an async function, an async generator or a module body.- Allowed in blocks, function bodies, module bodies,
forheads (for (using x = ...;;)),for-ofandfor await-ofheads. Not allowed directly as the body ofif,while, acaseclause or a labelled statement, and not at a script's top level (a module's is fine). The parser reports these with V8's headline (Unexpected identifier 'x'orUnexpected token 'using') and a label and help line that explain what to change. usingstays an identifier where the grammar needs one:using[x] = 1,using in obj,using\nx(a line break afterusingends the expression),for (using of of xs),for (using of = 0;;). An escaped spelling (us\u0069ng) is an identifier, as ECMA-262 5.1.5 requires; V8 accepts it as the keyword, a documented difference.- Resources are disposed in reverse order of declaration on every exit: normal completion,
break,continue,return,throw, a generator'sreturn()and a module's end. Null and undefined register nothing (forawait usingthey still cost oneawait). - A value that is not an object, or an object without
[Symbol.dispose]()(forawait using: neither[Symbol.asyncDispose]()nor[Symbol.dispose]()), throws aTypeErrorwhen the declaration runs. A non-callable method is also aTypeError. - If disposal throws while an error is already propagating, the later error becomes the
errorof aSuppressedErrorwhosesuppressedis the earlier one. Several failures nest in the order they happened.
Objects
SuppressedError(error, suppressed, message):errorandsuppressedare own data properties;messageis an own property only when given. When the engine makes one during disposal its message is V8'sAn error was suppressed during disposal.DisposableStackandAsyncDisposableStack:use,adopt,defer,move,dispose/disposeAsync,disposed,[Symbol.dispose]/[Symbol.asyncDispose]. After disposal their methods throwReferenceError: Cannot ... on an already-disposed ...in V8's wording (including V8'sDisposableStackwording for anAsyncDisposableStack).Symbol.disposeandSymbol.asyncDispose;Iterator.prototype[Symbol.dispose]callsreturn();AsyncIterator.prototype[Symbol.asyncDispose]awaits it.
Iterator additions
Iterator.prototype.chunks(n)yields arrays ofnitems (the last may be shorter).windows(n, undersized)yields sliding arrays ofn;undersizedis"only-full"(default) or"allow-partial".nmust be an integer from 1 to 4294967295 (RangeErrorotherwise,TypeErrorfor a non-number). A bad argument closes the receiver first.Iterator.prototype.includes(value, skippedElements)uses SameValueZero;skippedElementsis a non-negative integer orInfinity.Iterator.prototype.join(separator)joins likeArray.prototype.join(nullish items are empty).Iterator.zip(iterables, { mode, padding })andIterator.zipKeyed(object, options):modeis"shortest"(default),"longest"or"strict";"strict"throws aTypeErrorwhen the inputs differ in length;padding(longest only) supplies the filler values. All inputs are closed when the result is closed or throws.- Messages for these have no V8 counterpart in the Node release used as reference; they are plain statements of what was expected and what arrived, in the same style as the others.
Atomics.pause
Atomics.pause() returns undefined. The specification text takes no parameter (a hint, in the proposal, is an integer); the engine ignores arguments, where V8 throws a TypeError for a non-integer. There is nothing to wait for in a single agent, so no time passes.
Limits and replay
- Disposal runs inside the same fuel, depth and clock accounting as other script code; a
[Symbol.dispose]()that never returns ends in a Fuel limit report. Each scope that holds resources is charged against the allocation limit when its record is made. - Disposal is plain script execution, so record and replay need nothing extra: a replay re-runs the same disposals.
Reports
- A rejected declaration puts its label on the offending value (
`conn` is number 42, but `using` needs an object) and a help line that says what to write instead. - An error thrown by a dispose method has a note
thrown while disposingname, and, where the declaration is in the reported source, a secondary label on it (nameis disposed when its scope exits ``). - A
SuppressedErrorreport lists the chain, earlier errors first, one line each with the error name and message, the resource and the first script position:disposal failed N times; the errors, in the order they happened:. At most 20 entries are listed. The help line says where.errorand.suppressedare.