μ λλ μ΄ν° ν¨μκ° νΈμΆλλ©΄ μ λλ μ΄ν° κ°μ²΄κ° μμ±λλ€. μ΄λ symbol.iterator λ©μλλ₯Ό μμ λ°λ μ΄ν°λ¬λΈμ΄λ©΄μ, λμμ next λ©μλλ₯Ό κ°λ μ΄ν°λ μ΄ν°μ΄λ€.
κ·Έλ¬λ μ΄ν°λ μ΄ν°μλ μλ return, throw μ λ©μλλ₯Ό μΆκ°λ‘ κ°λλ€.
next λ₯Ό νΈμΆνλ©΄, yield ννμκΉμ§ μ½λλΈλ‘μ μ€ννκ³ yieldλ κ°μ valueνλ‘νΌν° κ°μΌλ‘, falseλ₯Ό done νλ‘νΌν° κ°μΌλ‘ κ°λ μ΄ν°λ μ΄ν° 리μ νΈ κ°μ²΄λ₯Ό λ°ννλ€.
return λ©μλλ₯Ό νΈμΆνλ©΄ μΈμλ‘ μ λ¬λ°μ κ°μ valueνλ‘νΌν° κ°μΌλ‘, true λ₯Ό done νλ‘νΌν° κ°μΌλ‘ κ°λ μ΄ν°λ μ΄ν° 리μ νΈ κ°μ²΄λ₯Ό λ°ννλ€.
throw λ©μλλ₯Ό νΈμΆνλ©΄ μΈμλ‘ μ λ¬λ°μ μλ¬λ₯Ό λ°μμν€κ³ , undefined λ₯Ό value νλ‘νΌν° κ°μΌλ‘, true λ₯Ό done νλ‘νΌν° κ°μΌλ‘ κ°λ μ΄ν°λ μ΄ν° 리μ νΈ κ°μ²΄λ₯Ό λ°ννλ€.
function* getFunc() {
try {
yield 1;
yield 2;
} catch(e) {
console.error(e);
}
}
const generator = getFunc();
console.log(generator.next()); {value:1 , done: false}
console.log(generator.next()); {value:2, done: false}
//console.log(generator.next()); {value:undefined, done: true}
console.log(generator.throw('Error!')); {value:"Error", done: true}
function* getFunc(){
const x = yield 1;
const y = yield (x+10);
return (x+y)*x;
}
const generator = getFunc();
let res = generator.next();
1. console.log(res); // {value: 1, done: false}
res = generator.next(100);
2. console.log(res); // {value: 110, done: false}
res = generator.next(30);
3. console.log(res); // {value: 13000, done: true}
//generatorμ λ΄λΆ λ³μμΈ xλ 첫λ²μ§Έ next() μ€νμμ yield κ°μ λ°ννλ€.
//λλ²μ§Έ next() μ€νμ μ λ¬λ μΈμκ° xμ κ°μΌλ‘ μ μλλ€.
const infiniteFibonacci = (function() {
let [pre,cur] = [0,1];
return {
[Symbol.iterator]() { return this; },
next() {
[pre,cur] = [cur,pre+cur];
return {value: cur};
}
};
}());
//infiniteFibonachi λ 무ν μ΄ν°λ¬λΈμ΄λ€.
for (const num of infiniteFibonacci) {
if(num > 10000) break;
console.log(num);
}
//μ λλ μ΄ν°λ‘ ꡬν
const infiniteFibonacci = (function* () {
let [pre, cur] = [0, 1];
while(true) {
[pre, cur] = [cur, pre + cur];
yield cur;
}
})();
for (const num of infiniteFibonacci) {
if (num > 10000) break;
console.log(num);
}
async function a(n) {return n*n};
a(10).then(res=>console.log(res)); //100
class MyClass{
async bar(n) {return n}
}
const myClass = new MyClass();
myClass.bar(10).then(res=>console.log(res)); //10
//κ°κ°μ async ν€μλλ‘ μΈν΄ λΉλκΈ° ν¨μμ²λΌ λμνλ€.
//ν¨μμ μ€ν κ²°κ³Όλ‘ ν¨μλ λ°νκ°μ resolveνλ νλ‘λ―Έμ€λ₯Ό λ°ννκ³
//then λ©μλμ 첫λ²μ§Έ μΈμλ‘ λκ²¨μ§ μ½λ°±μ΄ μ€νλλ€.
async function foo() {
//νλ©΄μ νλ²μ λ°μμ¬ λ!
const res = await Promise.all([
new Promise(res=>setTimeout(()=>res(1),3000)),
new Promise(res=>setTimeout(()=>res(2),2000)),
new Promise(res=>setTimeout(()=>res(3),1000)),
]);
console.log(res);
}
foo(); //[1, 2, 3] μ½ 3μ΄ μ λ κ±Έλ¦°λ€.
//Promise.all ν€μλλ₯Ό μ¬μ©ν κ²½μ°, λͺ¨λ λΉλκΈ° ν¨μκ° λ³λ ¬μ μΌλ‘ μ€νλλ€.
//μ²λ¦¬μκ°μ΄ κ°μ₯ μ€λ 걸리λ λΉλκΈ° μ²λ¦¬ ν¨μμ μνμκ°μ΄ μ 체 λΉλκΈ° μ²λ¦¬μ μκ°κ³Ό λΉμ·νλ€.
//λͺ¨λ μ²λ¦¬κ° λλνμλ μ²μμ λ΄κΈ΄ μμλλ‘ μ€νλ κ²°κ³Όκ°μ λ°°μ΄μ λ΄μ λ°ννλ€.
try {
setTimeout(()=>{throw new Error("err!")}, 1000);
} catch(e){
console.error('err', e);
}
async function a() {
try{
const url ="https://";
const res = await fetch(url)
const data = await res.json()
console.log(data);
}
catch(err){
console.error(err);
}
}
a();
//setTimeout ν¨μλ λΉλκΈ° ν¨μλ‘ ν¨μλ μ€νλλ μκ° μ½μ€νμμ μ κ±°λλ€.
//μ΄λ μ΄ ν¨μκ° μΈμλ‘ μ λ¬ν μ½λ°±ν¨μλ λΈλΌμ°μ κ° μ΄λ²€νΈλ₯Ό μμλ°μ μ€ννλ€.
//ν¨μμ λ°νκ°μΈ μλ¬λ μμ νΈμΆμλ₯Ό μ°Ύμ μ λ¬λλ€.
//μ½μ€νμλ ν΄λΉ μλ¬μ νΈμΆμλ‘ μμλλ μ£Όμ²΄κ° μμ΄ try catchλ¬Έμ΄ μ μμ μΌλ‘ μ€νλμ§ μλλ€.
//κ²°κ΅, μλ¬κ° λ°μν κ²μ catchνμ§ λͺ»νλ side effectμ΄ λ°μλλ€.
//μ€μ μ€ννλ©΄ error: message: "Uncaught Error: err!"κ° μ°ν