JavsScript59 Generator ; 함수의 실행을 중간에 멈췄다가 재개할 수 있는 기능 모델식 function* fn() { yield 1; yield 2; yield 3; return "finish"; } const a=fn(); a.next(); // 순차적으로 다음 차례 실행 literable -Symbol.literator 매소드가 있다 -Symbol.literator 는 Iterator를 반환해야 한다 literator -next 매소드를 가진다 -next 매소드는 value와 done 속성을 가진 객체를 반환한다 -작업이 끝나면 done은 true가 된다. 2022. 10. 2. async await ; 주문할 때 쓰는 promise chaining을 쓸 때보다 가속성이 좋아진다 async를 쓰면 프로미스를 반환한다. async function getName() { //return Promise.resolve("Tom"); throws new Error("err..."); } getName().then(name=> { console.log(name); }); //await 은 async 함수 내에서만 사용 가능 function getName(name) { return new Promise((resolve, reject) => { setTimeour(()=>{ resolve(name); } ); } async function showName() { const result = await getName("Mike"); con.. 2022. 10. 2. Promise ; 주문할 때 쓰는 const f1=(callback) => { setTimeout(function() { console.log("1번주문 완료"); callback(); }, 2000); }; const f2=(callback) => { setTimeout(function() { console.log("2번주문 완료"); callback(); },2000); }; const f3=(callback)=> { setTimeout(function() { console.log("3번주문 완료"); callback(); }, 2000); }; console.log('시작') f1(function() { f2(function() { f3(function() { console.log("끝") }); }); }); ===> 콜백 지옥에.. 2022. 10. 2. Promise ; 콜백함수 Promise ; 주문했을 때, 실패하거나 성공되었을 때 결정하는 콜백 함수 성공, 실패 const pr = new Promise((resolve, reject)=> { }); new Promise 는 스테이트와 리절트를 property로 받는다. state : pending (대기) result : undefined ------ resolve(vlaue) 가 되면, state : fulfilled(이행됨) result : value ------reject(error) 이 되면, state : rejected(거부됨) result : error 모델식 const pr = new Promise ((resolve, reject)=> { setTimeout(()=> { resolve('OK') }, 3000).. 2022. 10. 2. 이전 1 2 3 4 5 6 7 ··· 15 다음