PROMISE

์•ž์„  ๊ธ€์˜ ์ฝœ๋ฐฑ ์ง€์˜ฅ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ Promise ํŒจํ„ด์ด ์ œ์•ˆ๋˜์—ˆ๋‹ค.

jQuery์—์„œ๋Š” ์™„์ „ํ•˜์ง„ ์•Š์ง€๋งŒ PromiseํŒจํ„ด์ด Deferred ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.

Promise ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด 1. ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ณ ๋‚˜, ๋ณ‘๋ ฌ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๋“ฑ์˜ ์ œ์–ด๊ฐ€ ์ˆ˜์›”ํ•ด์ง€๊ณ  2. ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๋‹ค. ๋˜ํ•œ ๋‚ด๋ถ€์ ์œผ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ๊ตฌ์กฐ๊ฐ€ ํƒ„ํƒ„ํ•ด, ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ 3. ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด ๋ณด๋‹ค ์ง๊ด€์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

Promise๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณต(fulfilled)ํ–ˆ๋Š”์ง€, ์‹คํŒจ(rejected)ํ–ˆ๋Š”์ง€ ๋“ฑ์˜ ์ƒํƒœ ์ •๋ณด์™€ ์ฒ˜๋ฆฌ ์ข…๋ฃŒํ›„ ์‹คํ–‰๋  ์ฝœ๋ฐฑํ•จ์ˆ˜(then, catch)๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค.

Promise๋Š” ์ฃผ๋กœ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌํ˜„ํ•  ๋•Œ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ณ  ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

var _promise = function(param){
    return new Promise(function (){
        // setTimeoutํ•จ์ˆ˜๋กœ ๋น„๋™๊ธฐ 
        window.setTimeout(function(){
            if(param){
                resolve("ํ•ด๊ฒฐ์™„๋ฃŒ");
            }else{
                  reject(Error("์‹คํŒจ"));  
            }
        },30000);
    });
};

//promise ์‹คํ–‰
_promise(true).then(function(text){
    console.log(text);
},function(error){
    console.error(error);
});

//=> "ํ•ด๊ฒฐ ์™„๋ฃŒ"
function getData(callback) {
  // new Promise()
  return new Promise(function (resolve, reject) {
    $.get('url ์ฃผ์†Œ/products/1', function (response) {
      resolve(response);  // ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์œผ๋ฉด resolve()
    });
  });
}

// getData()์˜ ์‹คํ–‰์ด ๋๋‚˜๋ฉด ํ˜ธ์ถœ๋˜๋Š” then()
getData().then(function (data) {
  // resolve()์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด ์ „๋‹ฌ๋œ๋‹ค.
  console.log(data); // $.get()์˜ reponse ๊ฐ’์ด tableData์— ์ „๋‹ฌ๋จ
});

์œ„์˜ ์ฝ”๋“œ๋Š” Promise ์„ ์–ธ๊ณผ ์‹คํ–‰ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

Promise์˜ states

Promise๋Š” ์‚ฌ์ „์  ์˜๋ฏธ๋กœ "์•ฝ์†"์ด๋‹ค. js์—์„œ๋Š” "์ง€๊ธˆ์€ ์—†๋Š”๋ฐ ์ด์ƒ์—†์œผ๋ฉด ์ด๋”ฐ๊ฐ€ ์ฃผ๊ณ  ์—†์œผ๋ฉด ์•Œ๋ ค์ค„๊ฒŒ"๋ผ๋Š” ์•ฝ์†์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ promise๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํƒœ(state)๋ฅผ ๊ฐ€์ง„๋‹ค.

  1. pending(๋Œ€๊ธฐ) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋กœ์ง์ด ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ƒํƒœ. (์•ฝ์†์„ ์ˆ˜ํ–‰ ์ค‘์ธ ์ƒํƒœ)

  2. fulfilled(์ดํ–‰) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์–ด promise๊ฐ€ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค€ ์ƒํƒœ. (์•ฝ์†์ด ์ง€์ผœ์ง„ ์ƒํƒœ)

  3. rejected(์‹คํŒจ) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœ(์•ฝ์†์ด ๋ชป ์ง€์ผœ์ง„ ์ƒํƒœ)

  4. settled : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจ์ด๋“  ์„ฑ๊ณต์ด๋“  ๊ฒฐ๋ก ์ด ๋‚œ ์ƒํƒœ์ด๋‹ค.

๊ฐ ์ƒํƒœ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์‚ดํŽด๋ณด์ž.

Pending

new Promise();

๋‹ค์Œ๊ณผ ๊ฐ™์ด new Promise() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด pending ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

new Promise(function(resolve, reject){
    //...
});

๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ resolve, reject ์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

promsie๊ฐ€ ์ƒ์„ฑ๋œ ์งํ›„๋ถ€ํ„ฐ resolve๋‚˜ reject๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „๊นŒ์ง€์˜ ์ƒํƒœ์ด๋‹ค.

Fulfilled

new Promise(function(resolve, reject){
    resolve();
});

์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ž์ธ resolve๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•˜๋ฉด fulfilled(์ดํ–‰) ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. fulfilled์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด then() ์„ ์ด์šฉํ•ด ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

function getData(){
    return new Promise(function(resolve, reject){
        var data = 100;
        resolve(data);
    });
}

getData().then(function(resolvedData){
    console.log(resolvedData);
});

//=> 100

Rejected

new Promise(function(resolve,reject){
    reject();
});

์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ธ์ž์ธ reject๋กœ reject() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด rejected(์‹คํŒจ) ์ƒํƒœ๊ฐ€๋œ๋‹ค. rejected ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์‹คํŒจํ•œ ์ด์œ (์‹คํŒจ ์ฒ˜๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’)๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด catch() ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

function getData(){
    return new Promise(function(resolve,reject){
        reject(new Error("Request is failed"));
    });
}

getData.then().catch(function(err){
    console.log(err);
});

//=> "Request is failed"

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ catch() ๋ง๊ณ  then()๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

getData.then(function(){
    // ์„ฑ๊ณต...
},function(err){
   console.log(err);
});

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€๊ธ‰์ ์ด๋ฉด catch ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์™œ๋ƒํ•˜๋ฉด then()์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ๋‹ค์Œ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๋ฅผ ๊ฐ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

function getData(){
    return new Promise(function(resolve,reject){
        resolve('hi');
    });
}

getData.then(function(result){
    console.log(result);
    throw new Error("Error in then() ์ธ์ง€ ๋ชปํ•˜๋Š” ์˜ค๋ฅ˜");
},function(err){
    console.log('then error : ',error); 
});

๋” ๋งŽ์€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ƒํ™ฉ์„ ์œ„ํ•ด์„œ๋Š” catch()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์˜ˆ์ œ

function getData(){
    return new Promise(function(resolve, reject){
        $.get(url,function(response){
            if(response){
                resolve(response);
            }
            reject(new Error("Request is failed"));
        });
    });
}

getData().then(function(data){
    console.log(data);
}).catch(function(err){
    console.log(err);
})

์œ„์˜ ์ฝ”๋“œ๋Š” ์„œ๋ฒ„์—์„œ ์‘๋‹ต์„ ์ œ๋Œ€๋กœ ๋ฐ›์•„์˜ค๋ฉด resolve() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ, ์‘๋‹ต์ด ์—†์œผ๋ฉด reject() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค. ํ˜ธ์ถœ๋œ ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ then()์ด๋‚˜ catch()๋กœ ๋ถ„๊ธฐํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

Promise Chaining

์—ฌ๋Ÿฌ๊ฐœ์˜ Promise ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

function getData(){
    return new Promise({
        //...
    });
}

getData().then(function(data){
    //...
}).then(function(){
   //... 
}).then(function(){
   //... 
});

๋‹ค์Œ๊ณผ ๊ฐ™์ด then() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์—ฐ๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์‹œ

new Promise(function(resolve, reject){
    setTimeout(function(){
        resolve(1);
    },2000);
})
.then(function(result){
    console.log(result); //=>1
    return result+10; 
})
.then(function(result){
    console.log(result); //=>11
    return result+10;
})
.then(function(result){
    console.log(result); //=>21

});

์‹ค๋ฌด์—์„œ ์žˆ์„ ๋ฒ•ํ•œ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ›์•„์™€ ํŒŒ์‹ฑ, ์ธ์ฆ ๋“ฑ์˜ ์ž‘์—…์„ ๊ฑฐ์น˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค.

var userInfo = {
    id: 'test@xx.xxx',
    pw: '11111'
};

function parseValue(){
    return new Promise({
        //...
    });
}
function auth(){
    return new Promise({
        //...
    });
}
function display(){
    return new Promise({
        //...
    });
}
getData(userInfo)
    .then(parseValue)
    .then(auth)
    .then(display);

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ”„๋กœ๋ฏธ์Šค๋ฅผ .then() ์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Promise.all

์—ฌ๋Ÿฌ๊ฐœ์˜ ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์ด ์กด์žฌํ•˜๊ณ  ๊ทธ ์ž‘์—…๋“ค์ด ๋ชจ๋‘ ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๋‹ค์Œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ํ™œ์šฉํ•˜๋ฉด๋œ๋‹ค.

var _promise = new Promise(function(resolve, reject){
    window.setTimeout(function(){
        console.log("ํ•ด๊ฒฐ");
        resolve("first");
    },Math.random()*20000+1000);
});

var _promise2 = new Promise(function(resolve, reject){
    window.setTimeout(function(){
        console.log("ํ•ด๊ฒฐ2");
        resolve("second");
    },Math.random()*10000+1000);
});

Promise.all([_promise, _promise2]).then(function(values){
    console.log("๋ชจ๋‘ ์™„๋ฃŒ",values);
});

//=> "ํ•ด๊ฒฐ2"
//=> "ํ•ด๊ฒฐ"
//=> "๋ชจ๋‘ ์™„๋ฃŒ"

์ฐธ์กฐํŽ˜์ด์ง€

Last updated