In a few months, the updated version of ECMA Script will become standard. Let’s have a look at some of the features that will be included in ES2022.
ES2022’s characteristics:
The indexable values method .at()
This method allows us to read an element from a specified index. It can read elements from the end of a datatype using negative indexes.
Example:
[1,2,3,4,5].at(2) // returns 2 [1,2,3,4,5].at(-4) // returns 2
Await modules at the top level
We may now utilize await at the module level, eliminating the need to employ async functions or methods.
- Dynamically loading modules
const chat = await import(`./chat-${language}.mjs`);
- If module loading fails, a fallback is used.
let loading; try { loading = await import('https://first.example.com/loading'); } catch { loading = await import('https://second.example.com/loading'); }
- Using the resource that loads the fastest
const response = await Promise.any([ fetch('http://example.com/first.txt') .then(response => response.text()), fetch('http://example.com/second.txt') .then(response => response.text()), ]);