The Ecma Technical Committee 39 (TC39), the body responsible for the evolution of JavaScript (ECMAScript), recently advanced nine proposals through its stage process, with three new language features becoming part of the standard: Array.fromAsync, Error.isError, and explicit resource management with using
.
Array.fromAsync
is a utility for creating arrays from asynchronous iterables. This simplifies collecting data from sources like asynchronous generators or streams, eliminating the need for manual for await...of
loops.
The feature explainer provides the following real-world example from the httptransfer module:
async function toArray(items) {
const result = [];
for await (const item of items) {
result.push(item);
}
return result;
}
it('empty-pipeline', async () => {
const pipeline = new Pipeline();
const result = await toArray(
pipeline.execute(
[ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(
result,
[ 1, 2, 3, 4, 5 ],
);
});
With the new syntax, this becomes:
it('empty-pipeline', async () => {
const pipeline = new Pipeline();
const result = await Array.fromAsync(
pipeline.execute(
[ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(
result,
[ 1, 2, 3, 4, 5 ],
);
});
The Error.isError()
method also advances to Stage 4, providing a reliable way to check if a value is an error instance. The alternative instanceof Error
was considered unreliable because it will provide a false negative with a cross-realm (e.g., from an iframe, or node’s vm
modules) Error
instance.
Another proposal reaching Stage 4 is Explicit Resource Management, introducing a using
declaration for managing resources like files or network connections that need explicit cleanup. This proposal is motivated in particular by inconsistent patterns for resource management: iterator.return()
for ECMAScript Iterators, reader.releaseLock()
for WHATWG Stream Readers, handle.close()
for NodeJS FileHandles, and more.
There are also several footguns that the proposal alleviates. For instance, when managing multiple resources:
const a = ...;
const b = ...;
try {
...
}
finally {
a.close();
b.close();
}
Import Attributes (formerly Import Assertions) advances to Stage 3. This feature allows developers to add metadata to import declarations to provide information about the expected type of the module, such as JSON or CSS.
Other proposals moving forward at various stages include Promise.try
, aimed at simplifying error handling in promise chains, RegExp.escape
for safely escaping strings within regular expressions, and more. Developers may review the full list in a blog article online.
TC39 is the committee that evolves JavaScript. Its members include, among others, all major browser vendors. Each proposal for an ECMAScript feature goes through the following maturity stages:
- Stage 0: Strawman
- Stage 1: Proposal
- Stage 2: Draft
- Stage 3: Candidate
- Stage 4: Finished
A feature will be included in the standard once its proposal has reached stage 4 and thus can be used safely. Browser support may however lag behind adoption of the features in the standard.