ES6+ Basic5
์ธํ๋ฐ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ๊ณผ JavaScript ES6+ / ์ ์ธ๋
callback๊ณผ Promise
callback : ์ธ์์ callbackํจ์๋ฅผ ๋ฐ์์ ์ ์ํจ
function add10(a, callback) {
setTimeout(() => callback(a + 10), 100);
}
add10(5, res => {
// ์ฐ์์คํ
add10(res, res => {
log(res);
})
})
Promise : Promise๋ฅผ ๋ง๋ค์ด์ returnํด์ค
function add20(a) {
return new Promise(resolve => setTimeout(() => resolve(a+20), 100));
}
add20(5)
// ์ฐ์์คํ
.then(add20)
.then(log);
callback๊ณผ Promise์ ์ฐจ์ด
Promise๋ ์ผ๊ธ ๊ฐ์ผ๋ก ๋น๋๊ธฐ ์ํฉ์ ๋ค๋ฃธ(์ผ๊ธ = ์ธ์, ๋ณ์, ๋ฆฌํด๊ฐ ๋ฑ์ผ๋ก ์ฌ์ฉ๋ ์ ์์)
Promise๋ผ๋ ํด๋์ค๋ฅผ ํตํด ๋ง๋ค์ด์ง ์ธ์คํด์ค๋ฅผ ๋ฐํํ๋๋ฐ, ๋๊ธฐ,์ฑ๊ณต,์คํจ๋ฅผ ๋ค๋ฃจ๋ ์ผ๊ธ๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์
Promise๋ ์ฝ๋๋ฅผ ํ๊ฐํ์๋ ์ฆ์ Promise๊ฐ์ฒด๊ฐ ๋ฐํ๋จ
Promise๋ ๊ฐ์ฒด๋ฅผ ์ด์ฉํด ์ดํ์ ์ผ๋ค์ ์ฐ๊ฒฐ์ง์ด์ ํด๋๊ฐ ์ ์์
callback์ ๋น๋๊ธฐ ์ํฉ์ ์ฝ๋์ ์ปจํ ์คํธ๋ก๋ง ๋ค๋ฃธ(์คํํ๊ณ ๋๋ฉด ์๋ฌด๊ฒ๋ ์ด์ด๋๊ฐ ์ ์์)
๊ฐ์ผ๋ก์ Promise ํ์ฉ
const deley100 = a => new Promise(resolve =>
setTimeout(() => resolve(a), 100));
const go1 = (a, f) => a instanceof Promise ? .then(f) : f(a);
const add5 = a => a + 5;
const n1 = 10;
log(go1(go1(n1, add5), log));
const n2 = deley100(10);
log(go1(go1(n2, add5), log));
ํจ์ํฉ์ฑ ๊ด์ ์์์ Promise์ ๋ชจ๋๋
ํจ์ํฉ์ฑ :
f.g == f(g(f))
๋ชจ๋๋ : ํจ์ ํฉ์ฑ์ ํ ๋ ์์ ํ๊ฒ ํฉ์ฑํ ์ ์๊ฒ ํ๊ธฐ ์ํ ๋๊ตฌ(์ค์X)
Promise : ๋น๋๊ธฐ ์ํฉ์ ์์ ํ๊ฒ ํฉ์ฑํ๋ ๊ฒ
Kleisli Composition ๊ด์ ์์์ Promise
f.g
f(g(x)) = f(g(x))
์ค๋ฅ๊ฐ ๋ฌ์ ๋๋ ์ด ํฉ์ฑ์ด ์ฑ๋ฆฝ๋์ง ์์Kleisli Composition : ํน์ ํ ๊ท์น์ ๋ง๋ค์ด์ ํฉ์ฑ์ ์์ ํ๊ฒ ๋ง๋ค๊ณ ์ํ์ ์ผ๋ก ๋ฐ๋ผ๋ณผ ์ ์๊ฒ ๋ง๋ค์ด ์ฃผ๋ ๊ฒ
f(g(x)) = g(x)
: g์์ ์๋ฌ๊ฐ ๋ ๊ฒฝ์ฐ์๋ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ฑ๋ฆฝ๋ ์ ์์(Kleisli Composition)(๊ฐ์ Promise๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฏ๋ก ์์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ฑ๋ฆฝ๋จ)
Promise.reject
: ์๋ฌ๊ฐ ๋ ๊ฒฝ์ฐ reject๋ Promise๋ฅผ ๋ฆฌํดํจ
go, pipe, reduce์์ ๋น๋๊ธฐ ์ ์ด
ex) test/es6_code.html - 39 line go1()
Promise.then์ ์ค์ํ ๊ท์น
์๋ฌด๋ฆฌ ์ฌ๋ฌ๋ฒ์ Promise๊ฐ ์ค์ฒฉ๋์ด๋ ์์ชฝ์ ์๋ '๊ฐ'์ ํ๋ฒ์ then์ผ๋ก ๊บผ๋ผ ์ ์์
Promise.resolve(Promise.resolve(Promise.resolve(1))).then(log);
// 1
new Promise(resolve => resolve(new Promise(resolve => resolve(1))))).then(log);
// 1
์ง์ฐ ํ๊ฐ + Promise - L.map, map, take
์ง์ฐํ๊ฐ์ ๋น๋๊ธฐ ๋์์ฑ์ ์ง์ํ๋ ํจ์
const go1 = (a, f) => a instanceof Promise ? a.then(f) : f(a);
const take = curry((l, iter) => {
let res = [];
// ์ฌ๊ท
return function recur() {
for (const a of iter) {
if (a instanceof Promise) return a
.then(a => (res.push(a), res).length == l ? res : recur())
// Kleisli Composition ํ์ฉ
.catch(e => e == nop ? recur() : Promise.recject(e));
res.push(a);
if (res.length == l) return res;
}
return res;
} ();
});
L.map = curry(function *(f, iter) {
for(const a of iter) {
yield go1(a, f);
});
go(
// [1, 2, 3]
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)],
// L.map(a => a + 10),
// L.map(a => Promise.resolve(a + 10)),
// map์ L.map, takeAll์ ํ๋ ํจ์
map(a => Promise.resolve(a + 10)),
// takeAll,
log);
Kleisli Composition - L.filter, filter, nop, take
ํํฐ์์ ์ง์ฐํ๊ฐ์ ๋น๋๊ธฐ ๋์์ฑ, Promise๋ฅผ ์ง์ํ๋ ค๋ฉด Kleisli Composition์ ํ์ฉํด์ผํจ
Promise.reject()
๋ฅผ ํ๊ฒ๋๋ฉด ๋ชจ๋.then
์ ๋ฌด์ํ๊ณ.catch
๋ก ๊ฐ
// ์๋ฌด์ผ๋ ํ์ง์๋๋ค๋ ๊ตฌ๋ถ์
const nop = Sybol('nop');
L.filter = curry(function *(f, iter) {
for(const a of iter) {
const b = go1(a, b);
if (b isinstanceof Promise) yield b
.then(b => b ? a : Promise.reject(nop));
else if (f(b)) yield a;
}
});
go([1, 2, 3, 4, 5, 6],
L.map(a => Promise.resolve(a * a)),
L.filter(a => a % 2),
L.map(a => a * a),
take(4),
log);
reduce์์ nop์ง์
์ง์ฐํ๊ฐ์ ๋น๋๊ธฐ ๋์์ฑ์ ์ง์ํ๋ reduce
const reduceF = (acc, a, f) =>
a instanceof Promise ?
a.then(a => f(acc, a), e => e == nop ? acc : Promise.recject(e)) :
f(acc, a);
const head = iter => go1(take(1, iter), ([h]) => h);
const reduce = curry((f, acc, iter) => {
if (!iter) return reduce(f, head(iter = acc[Symbol.iterator](), iter);
iter = iter([Symbol.iterator]());
return go1(acc, function recur(acc) {
let cur;
while(!(cur = iter.next()).done) {
acc = reduceF(acc, cur.value, f);
if (acc instanceof Promise) return acc.then(recur);
}
});
return acc;
});
go([1, 2, 3, 4],
L.map(a => Promise.resolve(a * a)),
L.filter(a => Promise.resolve(a % 2)),
reduce(add),
log);
์ง์ฐ ํ๊ฐ + Promise์ ํจ์จ์ฑ
๋ชจ๋ ๊ฐ์ ํ๊ฐํ๊ณ ๊ทธ ์ค์์
take
ํ๋๊ฒ ์๋๋ผ ์ํ๋ ๊ฐ์take
ํ๊ณ ๋๋ฉด ๋๋จธ์ง๋ ์์ ํ๊ฐํ์ง ์์
Last updated