์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ์ ๋ํ ์ข ํฉ ๊ฐ์ด๋. ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ, ๋น๋๊ธฐ ์ดํฐ๋ ์ด์ , ๊ทธ๋ฆฌ๊ณ ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ์ ์ํ ๊ณ ๊ธ ํ์ฉ ์ฌ๋ก๋ฅผ ๋ค๋ฃน๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ: ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ๊ณผ ๋น๋๊ธฐ ์ดํฐ๋ ์ด์ ๋ง์คํฐํ๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ๋ ์ดํฐ๋ ์ด์ (๋ฐ๋ณต)์ ์ ์ดํ๊ณ ๋น๋๊ธฐ ์์ ์ ๊ด๋ฆฌํ๋ ๊ฐ๋ ฅํ ๋ฉ์ปค๋์ฆ์ ์ ๊ณตํฉ๋๋ค. ์ ๋๋ ์ดํฐ๋ ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ์ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ถ๋๋ฉฐ, ์ด๋ฅผ ํ์ฅํ์ฌ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ์ํํ๊ฒ ์ฒ๋ฆฌํฉ๋๋ค. ์ด ๊ฐ์ด๋๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ์ ํต์ฌ ๊ฐ๋ , ๊ณ ๊ธ ๊ธฐ๋ฅ, ๊ทธ๋ฆฌ๊ณ ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ์์์ ์ค์ ์ ์ฉ ์ฌ๋ก๋ฅผ ํฌ๊ด์ ์ผ๋ก ์ดํด๋ด ๋๋ค.
์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ ์ดํดํ๊ธฐ
์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ์ ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ๋ฐ๋ณตํ ์ ์๋์ง๋ฅผ ์ ์ํ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ๋๋ค. ์ฌ๊ธฐ์๋ ๋ ๊ฐ์ง ํต์ฌ ์์๊ฐ ํฌํจ๋ฉ๋๋ค:
- Iterable (๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด): ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฐํํ๋ ๋ฉ์๋(
Symbol.iterator)๋ฅผ ๊ฐ์ง ๊ฐ์ฒด์ ๋๋ค. - Iterator (์ดํฐ๋ ์ดํฐ):
next()๋ฉ์๋๋ฅผ ์ ์ํ๋ ๊ฐ์ฒด์ ๋๋ค.next()๋ฉ์๋๋ ๋ ๊ฐ์ง ์์ฑ์ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค:value(์ํ์ค์ ๋ค์ ๊ฐ)์done(๋ฐ๋ณต ์๋ฃ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ๋ฆฌ์ธ ๊ฐ).
๊ฐ๋จํ ์์๋ฅผ ํตํด ์ด๋ฅผ ์ค๋ช ํด ๋ณด๊ฒ ์ต๋๋ค:
const myIterable = {
data: [1, 2, 3],
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
if (index < this.data.length) {
return { value: this.data[index++], done: false };
} else {
return { value: undefined, done: true };
}
}
};
}
};
for (const value of myIterable) {
console.log(value); // Output: 1, 2, 3
}
์ด ์์์์ myIterable์ Symbol.iterator ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด์
๋๋ค. Symbol.iterator ๋ฉ์๋๋ 1, 2, 3 ๊ฐ์ ํ ๋ฒ์ ํ๋์ฉ ์์ฑํ๋ next() ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ดํฐ๋ ์ดํฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ๋ ์ด์ ๋ฐ๋ณตํ ๊ฐ์ด ์์ผ๋ฉด done ์์ฑ์ true๊ฐ ๋ฉ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ ์๊ฐ
์ ๋๋ ์ดํฐ๋ ์คํ์ ์ผ์ ์ค์งํ๊ณ ์ฌ๊ฐํ ์ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ํน๋ณํ ์ข
๋ฅ์ ํจ์์
๋๋ค. ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ํธ์ถ์ ๊ฑธ์ณ ์ํ๋ฅผ ์ ์งํ๋ ํจ์๋ฅผ ์์ฑํ์ฌ ๋ฐ๋ณต ์๊ณ ๋ฆฌ์ฆ์ ์ ์ํ ์ ์์ต๋๋ค. ์ ๋๋ ์ดํฐ๋ function* ๊ตฌ๋ฌธ๊ณผ yield ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋ค์์ ๊ฐ๋จํ ์ ๋๋ ์ดํฐ ์์์ ๋๋ค:
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}
const generator = numberGenerator();
console.log(generator.next()); // Output: { value: 1, done: false }
console.log(generator.next()); // Output: { value: 2, done: false }
console.log(generator.next()); // Output: { value: 3, done: false }
console.log(generator.next()); // Output: { value: undefined, done: true }
numberGenerator()๋ฅผ ํธ์ถํ๋ฉด ํจ์ ๋ณธ๋ฌธ์ด ์ฆ์ ์คํ๋์ง ์์ต๋๋ค. ๋์ ์ ๋๋ ์ดํฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. generator.next()๋ฅผ ํธ์ถํ ๋๋ง๋ค ํจ์๋ yield ํค์๋๋ฅผ ๋ง๋ ๋๊น์ง ์คํ๋ฉ๋๋ค. yield ํค์๋๋ ํจ์๋ฅผ ์ผ์ ์ค์งํ๊ณ ์ฐ์ถ๋ ๊ฐ์ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ๋ค์์ next()๊ฐ ํธ์ถ๋๋ฉด ํจ์๋ ์ค๋จ๋ ์ง์ ๋ถํฐ ์คํ์ ์ฌ๊ฐํฉ๋๋ค.
์ ๋๋ ์ดํฐ ํจ์์ ์ผ๋ฐ ํจ์์ ์ฐจ์ด์
์ ๋๋ ์ดํฐ ํจ์์ ์ผ๋ฐ ํจ์์ ์ฃผ์ ์ฐจ์ด์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- ์ ๋๋ ์ดํฐ ํจ์๋
function๋์function*์ ์ฌ์ฉํ์ฌ ์ ์๋ฉ๋๋ค. - ์ ๋๋ ์ดํฐ ํจ์๋
yieldํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์คํ์ ์ผ์ ์ค์งํ๊ณ ๊ฐ์ ๋ฐํํฉ๋๋ค. - ์ ๋๋ ์ดํฐ ํจ์๋ฅผ ํธ์ถํ๋ฉด ํจ์ ๊ฒฐ๊ณผ๊ฐ ์๋ ์ ๋๋ ์ดํฐ ๊ฐ์ฒด๊ฐ ๋ฐํ๋ฉ๋๋ค.
์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ๊ณผ ์ ๋๋ ์ดํฐ ํจ๊ป ์ฌ์ฉํ๊ธฐ
์ ๋๋ ์ดํฐ๋ ์๋์ผ๋ก ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ์ ๋ฐ๋ฆ
๋๋ค. ์ด๋ for...of ๋ฃจํ๋ ๋ค๋ฅธ ์ดํฐ๋ ์ดํฐ ์๋น ํจ์์์ ์ ๋๋ ์ดํฐ๋ฅผ ์ง์ ์ฌ์ฉํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
function* fibonacciGenerator() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const fibonacci = fibonacciGenerator();
for (let i = 0; i < 10; i++) {
console.log(fibonacci.next().value); // Output: The first 10 Fibonacci numbers
}
์ด ์์์์ fibonacciGenerator()๋ ํผ๋ณด๋์น ์์ด์ ์ฐ์ถํ๋ ๋ฌดํ ์ ๋๋ ์ดํฐ์
๋๋ค. ์ ๋๋ ์ดํฐ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ค์, ์ด๋ฅผ ๋ฐ๋ณตํ์ฌ ์ฒ์ 10๊ฐ์ ์ซ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค. ๋ฐ๋ณต์ ์ ํํ์ง ์์ผ๋ฉด ์ด ์ ๋๋ ์ดํฐ๋ ์์ํ ์คํ๋ ๊ฒ์
๋๋ค.
์ ๋๋ ์ดํฐ์ ๊ฐ ์ ๋ฌํ๊ธฐ
next() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋๋ ์ดํฐ์ ๊ฐ์ ๋ค์ ์ ๋ฌํ ์๋ ์์ต๋๋ค. next()์ ์ ๋ฌ๋ ๊ฐ์ yield ํํ์์ ๊ฒฐ๊ณผ๊ฐ ๋ฉ๋๋ค.
function* echoGenerator() {
const input = yield;
console.log(`You entered: ${input}`);
}
const echo = echoGenerator();
echo.next(); // Start the generator
echo.next("Hello, World!"); // Output: You entered: Hello, World!
์ด ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ next() ํธ์ถ์ ์ ๋๋ ์ดํฐ๋ฅผ ์์ํฉ๋๋ค. ๋ ๋ฒ์งธ next("Hello, World!") ํธ์ถ์ "Hello, World!" ๋ฌธ์์ด์ ์ ๋๋ ์ดํฐ์ ์ ๋ฌํ๊ณ , ์ด ๊ฐ์ input ๋ณ์์ ํ ๋น๋ฉ๋๋ค.
๊ณ ๊ธ ์ ๋๋ ์ดํฐ ๊ธฐ๋ฅ
yield*: ๋ค๋ฅธ ์ดํฐ๋ฌ๋ธ์ ์์ํ๊ธฐ
yield* ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค๋ฅธ ์ ๋๋ ์ดํฐ๋ฅผ ํฌํจํ ๋ค๋ฅธ ์ดํฐ๋ฌ๋ธ ๊ฐ์ฒด์ ๋ฐ๋ณต์ ์์ํ ์ ์์ต๋๋ค.
function* subGenerator() {
yield 4;
yield 5;
yield 6;
}
function* mainGenerator() {
yield 1;
yield 2;
yield 3;
yield* subGenerator();
yield 7;
yield 8;
}
const main = mainGenerator();
for (const value of main) {
console.log(value); // Output: 1, 2, 3, 4, 5, 6, 7, 8
}
yield* subGenerator() ๋ผ์ธ์ subGenerator()๊ฐ ์ฐ์ถํ๋ ๊ฐ๋ค์ mainGenerator()์ ์ํ์ค์ ํจ๊ณผ์ ์ผ๋ก ์ฝ์
ํฉ๋๋ค.
return() ๋ฐ throw() ๋ฉ์๋
์ ๋๋ ์ดํฐ ๊ฐ์ฒด์๋ ์ ๋๋ ์ดํฐ๋ฅผ ์กฐ๊ธฐ์ ์ข
๋ฃํ๊ฑฐ๋ ๊ทธ ์์ผ๋ก ์๋ฌ๋ฅผ ๋์ง ์ ์๋ return() ๋ฐ throw() ๋ฉ์๋๋ ์์ต๋๋ค.
function* exampleGenerator() {
try {
yield 1;
yield 2;
yield 3;
} finally {
console.log("Cleaning up...");
}
}
const gen = exampleGenerator();
console.log(gen.next()); // Output: { value: 1, done: false }
console.log(gen.return("Finished")); // Output: Cleaning up...
// Output: { value: 'Finished', done: true }
console.log(gen.next()); // Output: { value: undefined, done: true }
function* errorGenerator() {
try {
yield 1;
yield 2;
} catch (e) {
console.error("Error caught:", e);
}
yield 3;
}
const errGen = errorGenerator();
console.log(errGen.next()); // Output: { value: 1, done: false }
console.log(errGen.throw(new Error("Something went wrong!"))); // Output: Error caught: Error: Something went wrong!
// Output: { value: 3, done: false }
console.log(errGen.next()); // Output: { value: undefined, done: true }
return() ๋ฉ์๋๋ finally ๋ธ๋ก(์๋ ๊ฒฝ์ฐ)์ ์คํํ๊ณ done ์์ฑ์ true๋ก ์ค์ ํฉ๋๋ค. throw() ๋ฉ์๋๋ ์ ๋๋ ์ดํฐ ๋ด์์ ์๋ฌ๋ฅผ ๋ฐ์์ํค๋ฉฐ, ์ด๋ try...catch ๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์ก์ ์ ์์ต๋๋ค.
๋น๋๊ธฐ ์ดํฐ๋ ์ด์ ๊ณผ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ
๋น๋๊ธฐ ์ดํฐ๋ ์ด์ ์ ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ์ ํ์ฅํ์ฌ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ์ฒ๋ฆฌํฉ๋๋ค. ์ฌ๊ธฐ์๋ ๋ ๊ฐ์ง ์๋ก์ด ๊ฐ๋ ์ด ๋์ ๋ฉ๋๋ค:
- Async Iterable (๋น๋๊ธฐ ์ดํฐ๋ฌ๋ธ): ๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฐํํ๋ ๋ฉ์๋(
Symbol.asyncIterator)๋ฅผ ๊ฐ์ง ๊ฐ์ฒด์ ๋๋ค. - Async Iterator (๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ): Promise๋ฅผ ๋ฐํํ๋
next()๋ฉ์๋๋ฅผ ์ ์ํ๋ ๊ฐ์ฒด์ ๋๋ค. ์ด Promise๋ ๋ ๊ฐ์ง ์์ฑ์ ๊ฐ์ง ๊ฐ์ฒด๋ก ๊ท๊ฒฐ(resolve)๋ฉ๋๋ค:value(์ํ์ค์ ๋ค์ ๊ฐ)์done(๋ฐ๋ณต ์๋ฃ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ๋ฆฌ์ธ ๊ฐ).
๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ ๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ๋ฅผ ์์ฑํ๋ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ด๋ค์ async function* ๊ตฌ๋ฌธ๊ณผ await ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
async function* asyncNumberGenerator() {
await delay(1000); // Simulate an asynchronous operation
yield 1;
await delay(1000);
yield 2;
await delay(1000);
yield 3;
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
const asyncGenerator = asyncNumberGenerator();
for await (const value of asyncGenerator) {
console.log(value); // Output: 1, 2, 3 (with 1 second delay between each)
}
}
main();
์ด ์์์์ asyncNumberGenerator()๋ ๊ฐ ์ซ์ ์ฌ์ด์ 1์ด์ ์ง์ฐ์ ๋๊ณ ์ซ์๋ฅผ ์ฐ์ถํ๋ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ์
๋๋ค. for await...of ๋ฃจํ๋ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ๋ฐ๋ณตํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. await ํค์๋๋ ๊ฐ ๊ฐ์ด ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌ๋๋๋ก ๋ณด์ฅํฉ๋๋ค.
์๋์ผ๋ก ๋น๋๊ธฐ ์ดํฐ๋ฌ๋ธ ์์ฑํ๊ธฐ
๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๊ฐ ์ผ๋ฐ์ ์ผ๋ก ๋น๋๊ธฐ ์ดํฐ๋ฌ๋ธ์ ๋ง๋๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ด์ง๋ง, Symbol.asyncIterator๋ฅผ ์ฌ์ฉํ์ฌ ์๋์ผ๋ก ๋ง๋ค ์๋ ์์ต๋๋ค.
const myAsyncIterable = {
data: [1, 2, 3],
[Symbol.asyncIterator]() {
let index = 0;
return {
next: async () => {
await delay(500);
if (index < this.data.length) {
return { value: this.data[index++], done: false };
} else {
return { value: undefined, done: true };
}
}
};
}
};
async function main2() {
for await (const value of myAsyncIterable) {
console.log(value); // Output: 1, 2, 3 (with 0.5 second delay between each)
}
}
main2();
์ ๋๋ ์ดํฐ ๋ฐ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ์ ํ์ฉ ์ฌ๋ก
์ ๋๋ ์ดํฐ์ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ ๋ค์์ ํฌํจํ ๋ค์ํ ์๋๋ฆฌ์ค์์ ์ ์ฉํฉ๋๋ค:
- ์ง์ฐ ํ๊ฐ(Lazy Evaluation): ํ์ํ ๋ ๊ฐ์ ์์ฑํ์ฌ, ํนํ ๋์ฉ๋ ๋ฐ์ดํฐ์ ์ ๋ค๋ฃฐ ๋ ์ฑ๋ฅ์ ๊ฐ์ ํ๊ณ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ค์ผ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ์ ์ฒด ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ์ง ์๊ณ ๋์ฉ๋ CSV ํ์ผ์ ํ ์ค์ฉ ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
- ์ํ ๊ด๋ฆฌ: ์ฌ๋ฌ ํจ์ ํธ์ถ์ ๊ฑธ์ณ ์ํ๋ฅผ ์ ์งํ์ฌ ๋ณต์กํ ์๊ณ ๋ฆฌ์ฆ์ ๋จ์ํํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋ค์ํ ์ํ์ ์ ํ์ด ์๋ ๊ฒ์์ ๊ตฌํํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
- ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ: ์๋ฒ๋ก๋ถํฐ์ ๋ฐ์ดํฐ๋ ์ฌ์ฉ์ ์ ๋ ฅ๊ณผ ๊ฐ์ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ์ฒ๋ฆฌํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์ค์๊ฐ API์์ ๋ฐ์ดํฐ๋ฅผ ์คํธ๋ฆฌ๋ฐํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
- ์ ์ด ํ๋ฆ: ์ฝ๋ฃจํด๊ณผ ๊ฐ์ ์ฌ์ฉ์ ์ ์ ์ ์ด ํ๋ฆ ๋ฉ์ปค๋์ฆ์ ๊ตฌํํฉ๋๋ค.
- ํ ์คํ : ๋จ์ ํ ์คํธ์์ ๋ณต์กํ ๋น๋๊ธฐ ์๋๋ฆฌ์ค๋ฅผ ์๋ฎฌ๋ ์ด์ ํฉ๋๋ค.
๋ค์ํ ์ง์ญ์์์ ์์
์ ๋๋ ์ดํฐ์ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๊ฐ ์ฌ๋ฌ ์ง์ญ๊ณผ ์ํฉ์์ ์ด๋ป๊ฒ ์ฌ์ฉ๋ ์ ์๋์ง ๋ช ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค:
- ์ ์์๊ฑฐ๋(์ ์ธ๊ณ): ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฒฐ๊ณผ๋ฅผ ์ฒญํฌ ๋จ์๋ก ๊ฐ์ ธ์ค๋ ์ํ ๊ฒ์ ๊ธฐ๋ฅ์ ๊ตฌํํฉ๋๋ค. ์ด๋ฅผ ํตํด ๊ฒฐ๊ณผ๊ฐ ๋์ฐฉํ๋ ๋๋ก UI๋ฅผ ์ ์ง์ ์ผ๋ก ์ ๋ฐ์ดํธํ์ฌ ์ฌ์ฉ์์ ์์น๋ ๋คํธ์ํฌ ์๋์ ๊ด๊ณ์์ด ์ฌ์ฉ์ ๊ฒฝํ์ ๊ฐ์ ํ ์ ์์ต๋๋ค.
- ๊ธ์ต ์ ํ๋ฆฌ์ผ์ด์ (์ ๋ฝ): ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋๊ท๋ชจ ๊ธ์ต ๋ฐ์ดํฐ์ (์: ์ฃผ์ ์์ฅ ๋ฐ์ดํฐ)์ ์ฒ๋ฆฌํ์ฌ ๊ณ์ฐ์ ์ํํ๊ณ ๋ณด๊ณ ์๋ฅผ ํจ์จ์ ์ผ๋ก ์์ฑํฉ๋๋ค. ์ด๋ ๊ท์ ์ค์ ๋ฐ ์ํ ๊ด๋ฆฌ์ ๋งค์ฐ ์ค์ํฉ๋๋ค.
- ๋ฌผ๋ฅ(์์์): ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ GPS ์ฅ์น์์ ์ค์๊ฐ ์์น ๋ฐ์ดํฐ๋ฅผ ์คํธ๋ฆฌ๋ฐํ์ฌ ํ๋ฌผ์ ์ถ์ ํ๊ณ ๋ฐฐ์ก ๊ฒฝ๋ก๋ฅผ ์ต์ ํํฉ๋๋ค. ์ด๋ ๋ณต์กํ ๋ฌผ๋ฅ ๋ฌธ์ ๋ฅผ ๊ฐ์ง ์ง์ญ์์ ํจ์จ์ฑ์ ๊ฐ์ ํ๊ณ ๋น์ฉ์ ์ ๊ฐํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
- ๊ต์ก(์ํ๋ฆฌ์นด): ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝํ ์ธ ๋ฅผ ๋์ ์ผ๋ก ๊ฐ์ ธ์ค๋ ๋ํํ ํ์ต ๋ชจ๋์ ๊ฐ๋ฐํฉ๋๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ธํ๋ ํ์ต ๊ฒฝํ์ ์ ๊ณตํ๊ณ ๋์ญํญ์ด ์ ํ๋ ์ง์ญ์ ํ์๋ค์ด ๊ต์ก ์์์ ์ ๊ทผํ ์ ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
- ์๋ฃ(์๋ฉ๋ฆฌ์นด): ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฃ ์ผ์์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ์ฌ ํ๋ ฅ ์งํ๋ฅผ ๋ชจ๋ํฐ๋งํ๊ณ ์ค์๊ฐ์ผ๋ก ์ด์์ ๊ฐ์งํฉ๋๋ค. ์ด๋ ํ์ ์น๋ฃ๋ฅผ ๊ฐ์ ํ๊ณ ์๋ฃ ์ค๋ฅ์ ์ํ์ ์ค์ด๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
์ ๋๋ ์ดํฐ ์ฌ์ฉ์ ์ํ ๋ชจ๋ฒ ์ฌ๋ก
- ๋ฐ๋ณต ์๊ณ ๋ฆฌ์ฆ์ ์ ๋๋ ์ดํฐ ์ฌ์ฉ: ์ ๋๋ ์ดํฐ๋ ๋ฐ๋ณต ๋ฐ ์ํ ๊ด๋ฆฌ๊ฐ ํฌํจ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ ํฉํฉ๋๋ค.
- ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ ์ฌ์ฉ: ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ์ฒ๋ฆฌํ๊ณ ๋น๋๊ธฐ ์์ ์ ์ํํ๋ ๋ฐ ์ด์์ ์ ๋๋ค.
- ์ ์ ํ ์ค๋ฅ ์ฒ๋ฆฌ:
try...catch๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์ ๋๋ ์ดํฐ ๋ฐ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ ๋ด์ ์ค๋ฅ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค. - ํ์ํ ๋ ์ ๋๋ ์ดํฐ ์ข
๋ฃ: ํ์ํ ๋ ์ ๋๋ ์ดํฐ๋ฅผ ์กฐ๊ธฐ์ ์ข
๋ฃํ๋ ค๋ฉด
return()๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค. - ์ฑ๋ฅ ์ํฅ ๊ณ ๋ ค: ์ ๋๋ ์ดํฐ๋ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์ฑ๋ฅ์ ํฅ์์ํฌ ์ ์์ง๋ง ์ค๋ฒํค๋๋ฅผ ๋ฐ์์ํฌ ์๋ ์์ต๋๋ค. ์ ๋๋ ์ดํฐ๊ฐ ํน์ ์ฌ์ฉ ์ฌ๋ก์ ์ ํฉํ ์ ํ์ธ์ง ํ์ธํ๊ธฐ ์ํด ์ฝ๋๋ฅผ ์ฒ ์ ํ ํ ์คํธํฉ๋๋ค.
๊ฒฐ๋ก
์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ์ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ ํ๋์ ์ธ ์๋ฐ์คํฌ๋ฆฝํธ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ตฌ์ถํ๊ธฐ ์ํ ๊ฐ๋ ฅํ ๋๊ตฌ์
๋๋ค. ์ดํฐ๋ ์ดํฐ ํ๋กํ ์ฝ์ ์ดํดํ๊ณ yield ๋ฐ await ํค์๋๋ฅผ ๋ง์คํฐํจ์ผ๋ก์จ ๋ ํจ์จ์ ์ด๊ณ ์ ์ง๋ณด์ ๊ฐ๋ฅํ๋ฉฐ ํ์ฅ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ๋์ฉ๋ ๋ฐ์ดํฐ์
์ ์ฒ๋ฆฌํ๋ , ๋น๋๊ธฐ ์์
์ ๊ด๋ฆฌํ๋ , ๋ณต์กํ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํ๋ , ์ ๋๋ ์ดํฐ๋ ๊ด๋ฒ์ํ ํ๋ก๊ทธ๋๋ฐ ๊ณผ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ๋์์ ์ค ์ ์์ต๋๋ค.
์ด ์ข ํฉ ๊ฐ์ด๋๋ ์ ๋๋ ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ ์์ํ๋ ๋ฐ ํ์ํ ์ง์๊ณผ ์์๋ฅผ ์ ๊ณตํ์ต๋๋ค. ์์๋ฅผ ํตํด ์คํํ๊ณ , ๋ค์ํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ํ์ํ๋ฉฐ, ์ฌ๋ฌ๋ถ์ ํ๋ก์ ํธ์์ ์๋ฐ์คํฌ๋ฆฝํธ ์ ๋๋ ์ดํฐ์ ์ ์ฌ๋ ฅ์ ์ต๋ํ ๋ฐํํด ๋ณด์ญ์์ค.