๋น๋๊ธฐ์ฒ๋ฆฌ ํ ๋ ์ฌ์ฉํ๋ค.
continuation passing style - ํจ์๋ฅผ ์ง์์ ์ผ๋ก ๋๊ฒจ์ค๋ค.
function foo (a, b, c, callback) {
// ...
// compete : callback();
}
// foo -> bar -> baz
// ์ง์์ ์ผ๋ก ์ฝ๋ฐฑ์ ๋๊ฒจ์ฃผ๋ ๊ฒ์ด๋ค.
// callbackhell.com
new Promise => ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ค๋ค. ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ๋์์ค๋ค.
๊ฐ์ฒด๋ผ๋๊ฒ ๋ง๋ค์ด ์ก๋ค. ์์ ์ฅ ์ ์๋ ๋ฌผ๊ฑด์ด ๋ง๋ค์ด ์ก๋ค. ๋ถํ์ ํ๊ณ ์ค๋ฌผ๋ก ๊ฐ๊ฒ ๋์๋ค. ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ๊ฐ์ฒด์งํฅ๊ณผ ์ ๋ชฉํ์ฌ ๋น๋๊ธฐ๋ฅผ ๋ค๋ฅผ ์ ์๊ฒ ํ์๋ค.
์๋๋ ํจ์๋ก ๋ง๋ค๊ณ ๊ธฐ๋ค๋ฆฌ๊ธฐ๋ง ํ๋๋ฐ ์์ ๋๊ฐ ๋์ด๋๊ณ ๋ฅ๋์ ์ผ๋ก ํ ์ ์๊ฒ ๋์๋ค.
peomise๋ฅผ ์จ๋ callbackhell์ด ๋ํ๋ ์ ์๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ promise๊ฐ ํด๊ฒฐํด ์ค๋ค. ํ๋๊ฑด ์๋ชป๋๊ฑฐ๋ค.
๊ฒฐ๊ณผ๊ฐ์ ๋ชจ์์ ๊ณตํต์ ๋..
๋งคํํ๋ ํจ์๋ฅผ ํ๋ ๋ง๋ฌ..
var youtubePromise = new Promise(function (resolve, reject) { //new Promise ์ค๋ธ์ ํธ๋ค.
$.get({
url: SOME_URL,
success: function (data) {
resolve(data);
},
error: function (error) {
reject(error);
}
});
});
youtubePromise.then(function (data) {
console.log('Success:', data.items.length);
}).catch(function (error) {
console.error('Error:', error);
});