ES2022-top-level-await&array-at.md

๐Ÿก

ES2022 new features!

๊ด€๋ จ๊ธ€:
https://www.turing.com/kb/latest-javascript-features-in-es2022
https://dev.to/jasmin/whats-new-in-es2022-1de6

๐Ÿ“ธ 2022๋…„ 6์›” ํ™•์ •๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

- Await operator at the top-level // ์ตœ์ƒ์œ„ await(๋ชจ๋“ˆ ๋‚ด)
- Class field declarations // ์†์„ฑ์„ constructor ์•ˆ์— ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋จ
- Private methods and fields // #์„ ๋ถ™์—ฌ์„œ private ์‚ฌ์šฉ
- Static class fields and private static methods // static field ์‚ฌ์šฉ ๊ฐ€๋Šฅ
- Regexp Match Indices // ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Œ
- Ergonomic brand checks for private fields
- Array.prototype.at() function for Indexing // -1, ๋งˆ์ด๋„ˆ์Šค ์ธ๋ฑ์Šค
- Object.hasOwn() // ์ž๊ธฐ ์ž์‹ ์˜ ์†์„ฑ์ธ์ง€ ํ™•์ธ => boolean
- Temporal function
- Error Cause // ์—๋Ÿฌ ๊ฐ์ฒด์— ์›์ธ์„ ์ ์Œ `new Error('', {cause: ''});`

ES2022: top-level await โœจ

  • top-level await๋Š” ES2022์—์„œ ๋‚˜์˜จ ๊ธฐ๋Šฅ์ด๋ฉฐ, ๋ชจ๋“ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
    • ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ตœ์ƒ์œ„์—์„œ await๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
const res = await fetch(`https://jsonplaceholder.typicode.com/posts`);
const data = await res.json();
console.log(data);
โš ๏ธ ์ฃผ์˜์ 

- ์ตœ์ƒ๋‹จ await๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด promise๊ฐ€ resolve๋  ๋•Œ๊นŒ์ง€ ํ•˜๋‹จ ์ฝ”๋“œ๋ฅผ blocking ํ•ฉ๋‹ˆ๋‹ค.
- ๊ธฐ๋Šฅ์„ ์ •ํ™•ํžˆ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ์ตœ์ƒ๋‹จ await๋กœ ์ธํ•ด ์ฝ”๋“œ๊ฐ€ blocking ๋˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
    • console.log(lastPostAwait)๋Š” ์ƒ๋‹จ์˜ const lastPostAwait = await getLastPost();๊ฐ€ resolve๋  ๋•Œ๊นŒ์ง€ ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

      const getLastPost = async () => {
        const res = await fetch(`https://jsonplaceholder.typicode.com/posts`);
        const data = await res.json();
      
        return { title: data.at(-1).title, text: data.at(-1).body }; 
        // at() ๋˜ํ•œ ES2022์˜ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.
      };
      
      const lastPost = getLastPost();
      console.log(lastPost); // X, promise is pending
      
      lastPost.then((last) => console.log(last)); // X, not clean
      
      const lastPostAwait = await getLastPost();
      console.log(lastPostAwait); // O, use top-level await
      
  • ์ตœ์ƒ์œ„ await์˜ code blocking ์˜ˆ์‹œ
    • ์ตœ์ƒ์œ„ await๊ฐ€ ์žˆ๋Š” ๋ชจ๋“ˆ์„ importํ•˜๋ฉด, importํ•œ ๋ชจ๋“ˆ์€ ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋“ˆ์ด blocking code๋ฅผ ์™„๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

      image

      console.log("Export Linked");
      console.log("์š”์ฒญ ์‹œ์ž‘");
      
      const res = await fetch(`https://jsonplaceholder.typicode.com/posts`); // resolve๋  ๋•Œ๊นŒ์ง€ ์ฝ”๋“œ blocking
      const data = await res.json(); // resolve๋  ๋•Œ๊นŒ์ง€ ์ฝ”๋“œ blocking
      
      console.log("์š”์ฒญ ๋");
      
      export const lastData = { title: data.at(-1).title, text: data.at(-1).body }; // await ์—†์–ด๋„ ok
      // console.log(lastData) // ์ด๊ณณ์—์„œ๋„ ์ •์ƒ
      
      import * as FetchUsers from "./fetchUsers.js";
      
      console.log("Import");
      
      console.log("๋ฉ”์ธ ๋ชจ๋“ˆ ์‹œ์ž‘");
      console.log(FetchUsers.lastData); // link๋œ lastData ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
      

ES2022: Array.at()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.at

  • js์—์„œ๋Š” ํŒŒ์ด์ฌ ๋˜๋Š” R์ฒ˜๋Ÿผ array[-1]์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • bracket ๋‚ด๋ถ€์˜ ์Œ์ˆ˜ number๋Š” string์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. โ‡’ array[โ€-1โ€]
  • at์„ ์‚ฌ์šฉํ•˜๋ฉด, -1๋กœ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
array[array.length - 1]
array.at(-1)
  • 88.28% โ‡’ ์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

image