Iteration
๐Ÿ”Ž

Iteration

Description
Published
2024-02-02
Tags

Iteration์ด๋ž€?

Iteration์€ ๋œป ๊ทธ๋Œ€๋กœ ๋ฐ˜๋ณต์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ˆœํšŒ๋ฅผ ํ†ตํ•ด ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„  iteration ์ธํ„ฐํŽ˜์ด์Šค์— ์†ํ•ด ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.
iteration ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์†ํ•ด ์žˆ๋‹ค.
  • iterable ์ธํ„ฐํŽ˜์ด์Šค
  • iterator ์ธํ„ฐํŽ˜์ด์Šค
  • iteratorResult ์ธํ„ฐํŽ˜์ด์Šค
  • asyncIterable ์ธํ„ฐํŽ˜์ด์Šค
  • asyncIterator ์ธํ„ฐํŽ˜์ด์Šค
ย 

Iterable ์ธํ„ฐํŽ˜์ด์Šค

iterable ์ธํ„ฐํŽ˜์ด์Šค๋Š” @@iterator๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค.
์ด๋ฏธ ์ •์˜๋œ Symbol์ธ Symbol.iterator๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
์ด ํ•จ์ˆ˜๋Š” iterator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•œ iterator ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ย 

Iterator ์ธํ„ฐํŽ˜์ด์Šค

iterator ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” next๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
์ด ํ•จ์ˆ˜๋Š” iteratorResult ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•˜๋Š” iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ย 
ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์•ž์„  next ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด done ๊ฐ’์ด true์ธ iteratorResult ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ, ์ดํ›„ ํ˜ธ์ถœ๋˜๋Š” next ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ done ๊ฐ’์ด true์ธ iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.
ย 
์„ ํƒ์‚ฌํ•ญ์œผ๋กœ return๊ณผ throw๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•ด๋„ ๋œ๋‹ค.
๋‘ ํ•จ์ˆ˜๋„ iteratorResult ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•˜๋Š” iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ย 
return ํ•จ์ˆ˜๋Š” ์„ ํƒ์ ์œผ๋กœ ์ธ์ˆ˜์— ๊ฐ’์„ ๋ฐ›์•„ iteratorResult ๊ฐ์ฒด์˜ value ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๊ณ  done ํ”„๋กœํผํ‹ฐ์—๋Š” true๋ฅผ ํ• ๋‹นํ•ด์„œ iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.
return ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋” ์ด์ƒ next ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
ย 
throw ํ•จ์ˆ˜๋Š” ์„ ํƒ์ ์œผ๋กœ ์ธ์ˆ˜์— ๊ฐ’์„ ๋ฐ›์•„ done ํ”„๋กœํผํ‹ฐ์—๋Š” true๋ฅผ ํ• ๋‹นํ•ด์„œ iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.
์ธ์ˆ˜์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ Error ์ธ์Šคํ„ด์Šค๋ฅผ ํ• ๋‹นํ•œ๋‹ค.
ย 

IteratorResult ์ธํ„ฐํŽ˜์ด์Šค

iteratorResult ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๊ฐ€์ง€๋Š” done ํ”„๋กœํผํ‹ฐ์™€ ECMAScript language ๊ฐ’์„ ๊ฐ€์ง€๋Š” value ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค.
ECMAScript language ๊ฐ’์—๋Š” Undefined, Null, Boolean, String, Symbol, Number, BigInt, ๊ทธ๋ฆฌ๊ณ  Object๊ฐ€ ์†ํ•ด ์žˆ๋‹ค.
ย 

AsyncIterable ์ธํ„ฐํŽ˜์ด์Šค

asyncIterable ์ธํ„ฐํŽ˜์ด์Šค๋Š” @@asyncIterator๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค.
์ด ํ•จ์ˆ˜๋Š” asyncIterator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•œ asyncIterator ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
asyncIterator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•œ iterable ๊ฐ์ฒด์˜ ๋ฐ˜๋ณต ์ž‘์—…์€ for await (let item of iterable)ย ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
ย 

AsyncIterator ์ธํ„ฐํŽ˜์ด์Šค

asyncIterator ์ธํ„ฐํŽ˜์ด์Šค๋Š” next๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
์ด ํ•จ์ˆ˜๋Š” iteratorResult ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•˜๋Š” iteratorResult ๊ฐ์ฒด๋ฅผ ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์•ž์„  next ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด done ๊ฐ’์ด true์ธ iteratorResult ๊ฐ์ฒด๋ฅผ ๊ฐ์‹ผ ํ”„๋กœ๋ฏธ์Šค์ธ ๊ฒฝ์šฐ, ์ดํ›„ ํ˜ธ์ถœ๋˜๋Š” next ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ done ๊ฐ’์ด true์ธ iteratorResult ๊ฐ์ฒด๋ฅผ ๊ฐ์‹ผ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.
ย 
์„ ํƒ์‚ฌํ•ญ์œผ๋กœ return๊ณผ throw๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•ด๋„ ๋œ๋‹ค.
๋‘ ํ•จ์ˆ˜๋„ iteratorResult ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ค€์ˆ˜ํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ฐ์‹ผ iteratorResult ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
ย