[JS] MyPromise 만들기 : then 구현하기

0

Promise

목록 보기
1/1
post-thumbnail

MyPromise 연습 버전

  • 아래 코드가 비동기로 실행될 수 있도록 MyPromise 클래스를 만들어 보자!
 console.log("start")
  
  new MyPromise('hello')
    .then((v)=> v+' world' )
    .then((v)=> v+' and crong')
    .then((v)=> console.log(v));
 
  console.log("end")

/*--------출력 결과--------
"start"
"end"
"hello world and crong"
-----------------------*/
class MyPromise {
    constructor(initialStr) {
     this.cbList = [];
     setTimeout(()=> {
       this.cbList.reduce((acc,func) =>func(acc),initialStr)},0)
   
      then(cb) {
      this.cbList.push(cb)
      return this;
      }
    }
  
  console.log("start")
  
  new MyPromise('hello')
    .then((v)=> v+' world' )
    .then((v)=> v+' and crong')
    .then((v)=> console.log(v));
 
  console.log("end")

//출력
//"start"
//"end"
//"hello world and crong"

0개의 댓글