This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { curry2 } = Functional; | |
const then = curry2((f, a) => a instanceof Promise ? a.then(f) : f(a)); | |
const add10 = then(a => a + 10); | |
const add100 = then(a => a + 100); | |
const log = then(console.log); | |
log(add100(add10(num(5)))); // 즉시 115 | |
log(add100(add10(numP(6)))); // 1초 뒤 116 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="el1"></div> | |
<div id="el2"></div> | |
<script> | |
function loadImage(src) { | |
return new Promise(function(resolve) { | |
var image = new Image(); | |
image.onload = function() { resolve(image); }; | |
image.src = src; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mapC = curry2((f, coll) => | |
map(([a]) => a, | |
map(a => [f(a)], coll))); | |
go([1, 2, 3], | |
map(add10), | |
console.log); // 3초 뒤 [11, 12, 13] | |
go([4, 5, 6], | |
mapC(add10), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { mapC } = Functional; | |
let start; | |
const reset = _ => start = new Date(); | |
const check = _ => parseInt((new Date() - start) / 1000); | |
const add10 = a => new Promise(function(resolve) { | |
console.log(`들어온 시간: ${check()}초 뒤`); | |
setTimeout(function() { | |
resolve(a + 10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const team1 = { | |
name: 'italy', | |
skaters: [ | |
{ name: 'i1', time: 3000, und: 40 }, | |
{ name: 'i2', time: 2000, und: 30 }, | |
{ name: 'i3', time: 1000, und: 20 } | |
] | |
}; | |
const team2 = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Vanilla - Promise | |
function quarterfinal(teams) { | |
return Promise | |
.all( | |
teams.map(team => | |
function skateAll(skaters, i = 0) { | |
if (skaters.length == i) return; | |
return skate(skaters[i]).then(_=> skateAll(skaters, i+1)); | |
} (team.skaters) | |
.then(_=> console.log(`${team.name}팀 결승선 통과!`)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function numP(a) { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
resolve(a); | |
}, 1000); | |
}); | |
} | |
const concurrency = mapC(a => a()); | |
const series = map(a => a()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const add2 = a => Promise.resolve(a + 2); | |
(function() { | |
const list = [1, 2, 3, 4]; | |
const result = list.map(async function(val) { | |
return await add2(val); | |
}); | |
console.log(result, 'async/await 1'); | |
// [Promise, Promise, Promise, Promise] async/await 1 | |
}) (); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
const list = [1, 2, 3, 4]; | |
const result = await list.map(async function(val) { | |
return await add2(val); | |
}); | |
console.log(result, 'async/await 2'); | |
// [Promise, Promise, Promise, Promise] async/await 2 | |
}) (); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
const list = [1, 2, 3, 4]; | |
const result = await map(async function(val) { | |
return await add2(val); | |
}, list); | |
console.log(result, 'async/await 3'); | |
// [3, 4, 5, 6] async/await 3 | |
}) (); |