function getMult10Promise (number) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(number * 10);
}, 1000);
});
}
async function doAsyncWorks () {
const result1 = await getMult10Promise(1);
console.log(result1);
const result2 = await getMult10Promise(2);
console.log(result2);
const result3 = await getMult10Promise(3);
console.log(result3);
}
doAsyncWorks();
console.log('๐ก ์ด ๋ฌธ๊ตฌ๊ฐ ๋จผ์ ์ถ๋ ฅ๋จ');
// ๋น๋ฆฐ ๊ธ์ก์ผ๋ก ์ฝ์์ ํ๋ ํจ์
function moneyLend (borrow) {
return new Promise((resolve, reject) => {
console.log(`์ฑ๋ฌด ${borrow}๋ง์`);
setTimeout(() => {
if (Math.random() < 0.1) {
reject('์ฑ๋ฌด์ ํ์ฐ');
}
resolve(borrow * 1.1);
}, 1000);
});
}
async function lend5times () {
try {
const lend1 = await moneyLend(20);
const lend2 = await moneyLend(lend1);
const lend3 = await moneyLend(lend2);
const lend4 = await moneyLend(lend3);
const lend5 = await moneyLend(lend4);
console.log(`๐ฐ ๋ฐ๋ฉ ${lend5}๋ง์`);
} catch (msg) {
console.error(msg);
} finally{
console.log('- - ๋๊ธ์
์ข
๋ฃ - -');
}
}
lend5times();
const DEADLINE = 1400;
function getRelayPromise (name, start, failMsg) {
console.log(`๐ ${name} ์ถ๋ฐ`);
// ๐ก ๋๋ค ์๊ฐ๋งํผ ๋ฌ๋ฆฌ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๊ฒ ๋ค๋ ์ฝ์์ ๋ง๋ค์ด ๋ฐํ
return new Promise((resolve, reject) => {
const time = 1000 + Math.random() * 500;
setTimeout(() => {
if (time < DEADLINE) {
console.log(`๐ฉ ${name} ๋์ฐฉ - ${(start + time)/1000}์ด`);
resolve(start + time);
} else {
console.log(failMsg);
reject((start + time) / 1000);
}
}, time);
})
}
async function relay5 () {
try {
const time1
= await getRelayPromise('์ฒ ์', 0, '์ฒ ์๋ถํฐ ๊ดํ์
๋๋ค. ใ
ใ
');
const time2
= await getRelayPromise('์ํฌ', time1, '์ํฌ๊ฐ ์์ฃผํ์ง ๋ชปํ๋ค์.');
const time3
= await getRelayPromise('๋์ค', time2, '๋์ค์ด ๋ถ๋ฐํด๋ผ.');
const time4
= await getRelayPromise('์ ์', time3, '์ ์์๊ฒ ๋ฌด๋ฆฌ์๋๋ณด๋ค์.');
const time5
= await getRelayPromise('๊ธธ๋', time4, '์์, ์๊น์ต๋๋ค...');
} catch (msg) {
console.log(`๐ข ์์ฃผ ์คํจ - ${msg}์ด`);
} finally {
console.log('- - ๊ฒฝ๊ธฐ ์ข
๋ฃ - -');
}
}
relay5();
๋๊ธ ์์ญ