Promise - JeongWu/fe-javascript GitHub Wiki
Promise
javascript engine
ex) Google V8(chrome, nodeJS)
- Memory Heap: ๋ฉ๋ชจ๋ฆฌ ํ ๋น
- Call Stack: ์ฝ๋ ์คํ์ ๋ฐ๋ผ ํธ์ถ ์คํ์ด ์์
runtime
- web API: ๋ธ๋ผ์ฐ์ ์์ ์ ๊ณตํ๋ APIs
ex) setTimeout - Event Loop: Callback Event Queue์์ ํ๋์ฉ ๊บผ๋ด ๋์์ํค๋ loop
- Callback Queue: Event ์คํ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ Queue
๋น๋๊ธฐ ์ฒ๋ฆฌ
ํน์ ์ฝ๋์ ์ฐ์ฐ์ด ๋๋ ๋๊น์ง ์ฝ๋์ ์คํ์ ๋ฉ์ถ์ง ์๊ณ ๋ค์ ์ฝ๋๋ฅผ ๋จผ์ ์คํ
javascript ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ด์
- ๋จ์ผ ์ค๋ ๋ : ํ์๊ฐ ํ๋์ ์์ ๋ง ์ฒ๋ฆฌ ๊ฐ๋ฅ
- ์ฌ๋ฌ๊ฐ์ง event ์ฒ๋ฆฌํ ๋ ๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ฉด ํ๋์ event๊ฐ ๋ชจ๋ ์ฒ๋ฆฌ๋ ๋๊น์ง ๋ค๋ฅธ ์ด๋ค ์ ๋ฌด๋ ์ํX
ajax
function getData() {
var tableData;
$.get('https://domain.com/products/1', function(response) {
tableData = response;
});
return tableData;
}
console.log(getData()); // undefined
setTimeount()
// #1
console.log('Hello');
// #2
setTimeout(function() {
console.log('Bye');
}, 3000);
// #3
console.log('Hello Again'); //1,3,2
// setTimeout(callback, ms);
// ms == 0 ์ด๋ฉด ๋ฐ๋ก ์คํ๋์ง ์๊ณ ์ต์ ms ํ์ ์คํ๋จ ๋ค์ event loop์ ์คํ๋จ
setTimeout(function() {
// (A)
console.log('Second');
}, 0);
console.log('First');
// (B)
// First
// Second
promise
ES6์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ํจํด
call back
function asyncFunction1(cb) {
setTimeout(function() {
console.log(1)
cb()
}, 1000)
}
function syncFunction1(cb) {
console.log(1)
cb()
}
function syncFunction2() {
console.log(2)
}
asyncFunction1(function(asyncFunction1Result) {
syncFunction1(function(syncFunction1Result) {
syncFunction2()
})
})
// 1 2 3
-> but call back hell ๋ฐ์
function asyncFunction1(cb) {
setTimeout(function() {
console.log(1)
cb()
}, 100)
}
// ์ค๋ต
function asyncFunction9(cb) {
setTimeout(function() {
console.log(9)
cb()
}, 100)
}
function asyncFunction10(cb) {
setTimeout(function() {
console.log(10)
}, 100)
}
asyncFunction1(function(result) {
asyncFunction2(function(result) {
asyncFunction3(function(result) {
asyncFunction4(function(result) {
asyncFunction5(function(result) {
asyncFunction6(function(result) {
asyncFunction7(function(result) {
asyncFunction8(function(result) {
asyncFunction9(function(result) {
asyncFunction10()
})
})
})
})
})
})
})
})
})
// 1 2 3 4 5 6 7 8 9 10
์์ฑ
// Promise ๊ฐ์ฒด์ ์์ฑ
const promise = new Promise((resolve, reject) => {
// ๋น๋๊ธฐ ์์
์ ์ํํ๋ค.
if (/* ๋น๋๊ธฐ ์์
์ํ ์ฑ๊ณต */) {
resolve('result');
}
else { /* ๋น๋๊ธฐ ์์
์ํ ์คํจ */
reject('failure reason');
}
});
์ํ
- pending: ๋๊ธฐ์ค
- fulfilled: ์ดํ๋จ, ์ฑ๊ณต๋ฆฌ์ ์๋ฃ
- rejected: ์ฐ์ฐ์ด ์คํจ
ํ์ ์ฒ๋ฆฌ ๋ฉ์๋
- then: ์ฒซ ๋ฒ์งธ ์ฝ๋ฐฑ ํจ์๋ ์ฑ๊ณต ์ ํธ์ถ๋๊ณ ๋ ๋ฒ์งธ ์ฝ๋ฐฑ ํจ์๋ ์คํจ ์ ํธ์ถ
function get(url) {
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
}
;
}
);
}
get("https://jsonplaceholder.typicode.com/posts/1").then(resp=>console.log("resolve", resp), rej=>console.log("reject", rej), );
์๋ฌ ์ฒ๋ฆฌ ๋ฉ์๋
- catch: ์์ธ ๋ฐ์ํ๋ฉด ํธ์ถ
์ฐจ์ด์
- then: ๋น๋๊ธฐ ์ฒ๋ฆฌ์์ ๋ฐ์ํ ์๋ฌ(reject ํจ์๊ฐ ํธ์ถ ๋ ์ํ)๋ง์ ์บ์น
- catch: ๋น๋๊ธฐ ์๋ฌ ๋ฟ ์๋๋ผ then ๋ฉ์๋ ๋ด๋ถ์์ ๋ฐ์ํ ์๋ฌ
promise chain
ํ์ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฒด์ด๋ํ์ฌ ์ฌ๋ฌ๊ฐ์ ํ๋ก๋ฏธ์ค๋ฅผ ์ฐ๊ฒฐํ์ฌ ์ฌ์ฉ
new Promise((resolve,reject)=>{
console.log('Initial');
resolve();
}
).then(()=>{
throw new Error('Something failed');
console.log('Do this');
}
).catch(()=>{
console.log('Do that');
}
).then(()=>{
console.log('Do this whatever happened before');
}
);
// Initial
// Do that
// Do this whatever happened before
์ ์ ๋ฉ์๋
- Promise.resolve
์ฃผ์ด์ง ๊ฐ์ผ๋ก resolveํ๋ Promise๋ฅผ ์์ฑ
const resolvedPromise = Promise.resolve([1, 2, 3]);
resolvedPromise.then(console.log); // [ 1, 2, 3 ]
- Promise.reject
์ฃผ์ด์ง ๊ฐ์ผ๋ก rejectํ๋ promise๋ฅผ ์์ฑ
const rejectedPromise = Promise.reject(new Error('Error!'));
rejectedPromise.catch(console.log); // Error: Error!
- Promise.all
์ ๋ฌ๋ฐ์ ๋ชจ๋ ํ๋ก๋ฏธ์ค๋ฅผ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌํ๊ณ ๊ทธ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ๋ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํ - Promise.race
๊ฐ์ฅ ๋จผ์ ์ฒ๋ฆฌ๋ ํ๋ก๋ฏธ์ค๊ฐ resolveํ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ๋ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํ