ํฐ์คํ ๋ฆฌ ๋ทฐ
[Udemy] JavaScript: The Advanced Concepts - ์น์ 9:Asynchronous JavaScript
Carrot๐ฅ 2020. 8. 22. 21:18์น์ 9:Asynchronous JavaScript
132. Section Overview
//1
setTimeout(()=>{console.log('1', 'is the loneliest number')}, 0)
setTimeout(()=>{console.log('2', 'can be as bad as one')}, 10)
//2
Promise.resolve('hi').then((data)=> console.log('2', data))
//3
console.log('3','is a crowd')
// --output--
// 3 is a crowd
// 2 hi // Promise
// -- program return undefined --
// 1 is the loneliest number // setTimeout 0
// 2 can be as bad as one // setTimeout 10
Web API - DOM ์ด๋ฒคํธ ํธ๋ค๋ฌ, fetch(), setTimeout() ...
โ
135. Promises
A promis is an object that may produce a single value some time in the future.
either a resolved
value, or a reason that it’s not resolved (rejected
)
states : pending
/ fulfilled
/ rejected
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise
callback pyramid of dom
movePlayer(100, 'Left', function () {
movePlayer(400, 'Left', function () {
movePlayer(10, 'Right', function () {
movePlayer(330, 'Left', function () {
});
});
});
});
์ฝ๋ฐฑ์ ์ค์ฒฉ ์์
really really complicated code!
โ
promise exercise
const promise = new Promise((resolve, reject) => {
if (true) {
resolve('Stuff Worked');
} else {
reject('Error, it broke');
}
});
promise
.then(result => result + '!')
.then(result2 => {
console.log(result2);
})
์ถ๋ ฅ๊ฒฐ๊ณผ
Stuff Worked!
Promise {<fulfilled>: undefined}
then์ ์ฒซ๋ฒ์งธ ์ธ์๋ ์๋จ promise ์ resolve ๋ ๊ฒฐ๊ณผ๊ฐ ๋ด๊น
โ
promise
.then(result => {
//throw "์๋ฌ1"
return result + '!'
})
.then(result2 => {
throw "์๋ฌ2"
console.log(result2);
})
.catch((err) => console.log(err))
.catch
๊ฐ ์๋ฌ2 ์ก์์ ์ถ๋ ฅํจ
catch๊ตฌ๋ฌธ ์ด์ ์ ์๋ ์๋ฌ๋ง ์ก์์์์
โ
const promise = new Promise((resolve, reject) => {
if (false) {
resolve('Stuff Worked');
} else {
reject('Error, it broke');
}
});
// Uncaught (in promise) Error, it broke
.then
์ํด๋ reject๋๊ฐ ๋ฐ๋ก๋์ค๋น..?
โ
promise all
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'ONE');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'TWO');
});
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, 'THREE');
});
Promise.all([promise1, promise2, promise3])
.then(values => {
console.log(values);
})
(3000ms ํ) until promises were resolved
(3) ["ONE", "TWO", "THREE"]
โ
promise
๋จผ์ ์ ์ธํ๋ด์
3์ด ๋๊ธฐ ์ ์ promise.all.then
ํ๋ฉด pending ์ํ๋ก ๋์ด
3์ด ์ง๋๊ณ ๋์ all ํ๋ฉด ๋ฐ๋ก ์ถ๋ ฅ
โ
const urls = [
'https://jsonplaceholder.typicode.com/users',
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/albums',
'o_o?'
];
Promise.all(urls.map(url => {
return fetch(url).then(resp => resp.json());
}))
.then(results => {
console.log(results[0]);
console.log(results[1]);
console.log(results[2]);
})
.catch(() => console.log("ERROR!"));
// --output--
// Promise {<pending>}
// GET chrome-extension://laookkfknpbbblfpciffpaejjkokdgca/o_o? net::ERR_FILE_NOT_FOUND
// ERROR!
์ด์ํ url ์ด ์์ด์ fetch
์์ ์ค๋ฅ๊ฐ ๋๋ฉด catch
๋ก ์ก์ ์ ์์
โ
fetch('https://jsonplaceholder.typicode.com/users') // return Promise
โ
โ
136. ES8 - Async Await
async function
: a function that returns a promise.
benefit of async/await
is that it makes code easier to read.
- 1๋จ๊ณ : callback pyramid of dom
movePlayer(100, 'Left', function () {
movePlayer(400, 'Left', function () {
movePlayer(10, 'Right', function () {
movePlayer(330, 'Left', function () {
});
});
});
});
- 2๋จ๊ณ : Promise
movePlayer(100, 'Left')
.then(() => movePlayer(400, 'Left'))
.then(() => movePlayer(10, 'Right'))
.then(() => movePlayer(330, 'Left'))
- 3๋จ๊ณ : Async/Await
async function playerStart() {
const firstMove = await movePlayer(100, 'Left'); // pause
await movePlayer(400, 'Left'); // pause
await movePlayer(10, 'Right'); // pause
await movePlayer(330, 'Left'); // pause
}
โ
more realistic example (use fetch
function)
// Promise
fetch('https://jsonplaceholder.typicode.com/users')
.then(resp => resp.json())
.then(console.log)
// Async/Await
async function fetchUsers() {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const data = await response.json();
console.log(data);
}
++ response.json()
์ ๋ํด
response.json()
does return a promise as well (as it waits for the body to load)
Why does .json() return a promise? : https://stackoverflow.com/questions/37555031/why-does-json-return-a-promise
โ
more realistic example 2 (Promise.all
)
const urls = [
'https://jsonplaceholder.typicode.com/users',
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/albums',
];
// Promise.all
Promise.all(urls.map(url =>
fetch(url).then(resp => resp.json())
))
.then(results => {
console.log("Users : ", results[0]);
console.log("Posts : ", results[1]);
console.log("Albums : ", results[2]);
})
.catch("Oooops!");
// Async/Await
const getData = async function () {
const [users, posts, albums] =
await Promise.all(urls.map(url =>
fetch(url).then(resp => resp.json())
));
console.log("Users : ", users);
console.log("Posts : ", posts);
console.log("Albums : ", albums);
}
error handing
Promise.all
์์ ์ฒ๋ผ error ๋ฐ์ ์, catch ํ๋ ค๋ฉด
=> use try...catch
block!
// try...catch on Async/Await
const urls = [
'https://jsonplaceholder.typicode.com/users',
'weird url', // ์ฌ๊ธฐ์ ์๋ฌ๋ ๊ฑฐ์
'https://jsonplaceholder.typicode.com/albums',
];
const getData = async function () {
try {
const [users, posts, albums] =
await Promise.all(urls.map(url =>
fetch(url).then(resp => resp.json())
));
console.log("Users : ", users);
console.log("Posts : ", posts);
console.log("Albums : ", albums);
} catch (err) {
console.log("Oooops!", err)
}
}
// --output-- => Oooops! TypeError: Failed to fetch
async/await์ ์๋ก์ด ์ ํ์ค๋๊น although intimidating at first ํ๊ธด ํ์ง๋ง
์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์!!
โ
137. ES9 (ES2018)
์ ๋ฒ์ ์ ๋นํด์ ์์์ผํ three main new features ๊ฐ ์๋ค
Object spread operator
//Object spread operator
const animals = {
tiger: 23,
lion: 5,
monkey: 2,
quokka: 1204
}
const { tiger, ...restAnimals, quokka } = animals;
// SyntaxError: Rest element must be last element
const { tiger, ...restAnimals } = animals;
// --output--
// tiger -> 23
// restAnimals -> {lion: 5, monkey: 2, quokka: 1204}
function objectSpread(p1, p2, p3) {
console.log(p1);
console.log(p2);
console.log(p3);
}
const { tiger, lion, ...restAnimals } = animals;
objectSpread(tiger, lion, restAnimals);
// --output--
// 23
// 5
// {monkey: 2, quokka: 1204}
โ
138. ES9 (ES2018) - Async
finally
const urls = [
'http://swapi.dev/api/people/1/',
'http://swapi.dev/api/people/2/',
'http://swapi.dev/api/people/3/',
'http://swapi.dev/api/people/4/',
]
Promise.all(urls.map(url =>
fetch(url).then(person => person.json())
))
.then(people => {
console.log('1', people[0]);
throw Error;
console.log('2', people[1]);
console.log('3', people[2]);
console.log('4', people[3]);
})
.catch(err => console.log('ughhhhh fix it!', err))
.finally(() => console.log('extra'))
// --output--
// 1 {name: "Luke Skywalker", height: "172", mass: "77", hair_color: "blond", skin_color: "fair", …}
// ughhhhh fix it! ƒ Error() { [native code] }
// extra
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
finally()
๋ฉ์๋๋ Promise
๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
Promise๊ฐ ์ฒ๋ฆฌ๋๋ฉด ์ถฉ์กฑ๋๊ฑฐ๋ ๊ฑฐ๋ถ๋๋์ง ์ฌ๋ถ์ ๊ด๊ณ์์ด ์ง์ ๋ ์ฝ๋ฐฑ ํจ์๊ฐ ์คํ๋ฉ๋๋ค.
์ด๊ฒ์ Promise๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ํ ๋์๋์ง ๊ฑฐ์ ๋์๋์ง์ ๊ด๊ณ์์ด Promise
๊ฐ ์ฒ๋ฆฌ ๋ ํ์ ์ฝ๋๊ฐ ๋ฌด์กฐ๊ฑด ํ ๋ฒ์ ์คํ๋๋ ๊ฒ์ ์ ๊ณตํฉ๋๋ค.
์๊ฒ์ ์ฐ๋ ์ด์ : Promise์ then()
๊ณผ catch()
ํธ๋ค๋ฌ์์์ ์ฝ๋ ์ค๋ณต์ ํผํ๊ฒ ํฉ๋๋ค.
p.then(onFulfilled, onRejected)
์ธ ๊ฒ๊ณผ ๋ฌ๋ฆฌp.finally(onFinally)
์๋ ๊ฒ ์ฐ์.
onFinally์ ๋ค์ด๊ฐ๋ function์ ์ด๋ ํ ์ธ์๋ ์ ๋ฌ๋ฐ์ง ์๋๋ค.
์๋ํ๋ฉด ์ดํ/๊ฑฐ๋ถ ์ฌ๋ถ๋ฅผ ํ๋จํ ์ ์๊ธฐ ๋๋ฌธ์!
โ
for await of
const urls = [
'https://jsonplaceholder.typicode.com/users',
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/albums',
];
const getData = async function () {
try {
const [users, posts, albums] =
await Promise.all(urls.map(url => {
const response = await fetch(url);
return response.json();
}));
console.log("Users : ", users);
console.log("Posts : ", posts);
console.log("Albums : ", albums);
} catch (err) {
console.log('Ooooops!!', err);
}
}
// do exactly what this one does above by
// using by "for await of"
const getData2 = async function () {
const arrayOfPromises = urls.map(url => fetch(url));
for await (let request of arrayOfPromises) {
const data = await request.json();
console.log(data);
}
}
โ
Promise.all
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
๋ฐํํ ํ๋ก๋ฏธ์ค์ ์ดํ ๊ฒฐ๊ณผ๊ฐ์ (ํ๋ก๋ฏธ์ค๊ฐ ์๋ ๊ฐ์ ํฌํจํ์ฌ) ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ์ํ ๊ฐ๋ฅํ ๊ฐ์ฒด์ ํฌํจ๋ ๋ชจ๋ ๊ฐ์ ๋ด์ ๋ฐฐ์ด์ ๋๋ค.
์ฃผ์ด์ง ํ๋ก๋ฏธ์ค ์ค ํ๋๋ผ๋ ๊ฑฐ๋ถํ๋ฉด, ๋ค๋ฅธ ํ๋ก๋ฏธ์ค์ ์ดํ ์ฌ๋ถ์ ์๊ด์์ด ์ฒซ ๋ฒ์งธ ๊ฑฐ๋ถ ์ด์ ๋ฅผ ์ฌ์ฉํด ๊ฑฐ๋ถํฉ๋๋ค.
Promise.all
์ ์ฃผ์ด์ง ์ํ ๊ฐ๋ฅํ ๊ฐ์ฒด๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ์๋ง ๋๊ธฐ์ ์ผ๋ก ์ดํ๋ฉ๋๋ค.
var p = Promise.all([]); // ์ฆ์ ์ดํํจ
var p2 = Promise.all([1337, "hi"]); // ํ๋ก๋ฏธ์ค๊ฐ ์๋ ๊ฐ์ ๋ฌด์ํ์ง๋ง ๋น๋๊ธฐ์ ์ผ๋ก ์คํ๋จ
console.log(p);
console.log(p2);
setTimeout(function() {
console.log('the stack is now empty');
console.log(p2);
});
// ์ถ๋ ฅ
// Promise { <state>: "fulfilled", <value>: Array[0] } // p
// Promise { <state>: "pending" } // p2
// the stack is now empty
// Promise { <state>: "fulfilled", <value>: Array[2] } // p2
--- MDN ์์ ๊ฐ ๋งค์ฐ ๋์์ด ๋๋๊ตฌ๋งโบ
โ
139. Job Queue
Promise
๋ ์ต๊ทผ์ ์ถ๊ฐ๋์ด์
์ด ์ถ๊ฐ์ฌํญ์ ์์ฉaccommodate ํ๊ธฐ ์ํด์
๊ธฐ์กด์ Event Loop๋ฅผ ๋ณ๊ฒฝํด์ผํ๋ค.
ECMA script์์๋ Promise ๋ฅผ ์ํด์ ์๋ ๊ฐ์ง๊ณ ์๋ Callback Queue ์ ์ฌ์ฉํ๋ ๊ฒ ๋์ ์ ๋ค๋ฅธ ํ๊ฐ ํ์ํ๋ค๊ณ ํ๋ค.
๊ทธ๊ฑฐ์จ ๋ฐ๋ก ‘Job Queue’์ด๋ค! (ํน์ Microtask Queue)
Job Queue๋ ์ฝ๋ฐฑํ์ ๋นํด์ smaller ํ์ง๋ง ๋ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ค.
(Event Loop๊ฐ ์ฒ๋ฆฌํ ๋)
JAVASCRIPT RUNTIME ๋ค์ด์ด๊ทธ๋จ์ ๋ค์ ๋ณด์
Event Loop ๋ ๋จผ์ Jop Queue ๋ฅผ ์ฒดํนํ๊ณ
์ฝ์คํ์ด ๋น์์๋ ์๊ฑฐ ๋จผ์ ์ฒ๋ฆฌํ๋ค.
// Callback Queue
setTimeout(() => {
console.log('1', 'is the loneliest number')
}, 0);
setTimeout(() => {
console.log('2', 'can be as bad as one')
}, 0);
// 2 Job Queue - Microtask Queue
Promise.resolve('hi').then((data) => console.log('2', data));
// 3
console.log('3', 'is a crowd');
---output---
3 is a crowd >> ๋น๋๊ธฐ๊ฐ ์๋ ์ ๋ ๋จผ์ ์ฒ๋ฆฌ๋๊ณ
2 hi >> ์ด๋ฒคํธํ: ํค์ด์กํ!! ๋ญ๊ฐ๊ณ ์๋๊ฑฐ์๋? / ์กํ: ใ
ใ
์ด๊ฑฐ์์
undefined >> JS file ๋
1 is the loneliest number >> ์ด๋ฒคํธํ: ํค์ด์กํ!! ๋ญ๊ฐ๊ณ ์๋๊ฑฐ์๋? / ์กํ: ใดใด์์!! ์ด์ ์ฝ๋ฐฑํํํ
๊ฐ๋๋ผ!!
2 can be as bad as one
์ด๋ค ๋ ๊ฑฐ์ ๋ธ๋ผ์ฐ์ ๋ ์กํ๊ฐ ์๊ธฐ๋ํ์ง๋ง ๋๋ถ๋ถ ์คํ ๋ค๋๋ฅผ ๋ฐ๋ฅธ๋ค.
โ
140. Parallel, Sequence and Race
multiple promises๋ฅผ ์ฒ๋ฆฌํ๋๋ฐ ์์ด์ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
- in parallel (start all at the same time)
- sequential (they're all dependent on each other)
- race (one comes back first and ignores the rest.)
โ
const promisify = (item, delay) =>
new Promise((resolve) =>
setTimeout(() =>
resolve(item), delay));
const a = () => promisify('a', 100);
const b = () => promisify('b', 5000);
const c = () => promisify('c', 3000);
์์ 3๊ฐ์ multiple promises๋ฅผ ๊ฐ ๋ฐฉ๋ฒ์ผ๋ก ์ฒ๋ฆฌํด๋ณด์
โ
1) Parallel
async function parallel() {
const promises = [a(), b(), c()];
const [output1, output2, output3] = await Promise.all(promises);
return `parallel is done: ${output1} ${output2} ${output3}`
}
parallel().then(console.log);
// --output--
// (0์ดํ) Promise {<pending>}
// (5์ดํ) parallel is done: a b c
โ
2) Race
async function race() {
const promises = [a(), b(), c()];
const output1 = await Promise.race(promises);
return `race is done: ${output1}`;
}
race().then(console.log);
// -- output --
// ( 0์ดํ) Promise {<pending>}
// (0.1์ดํ) race is done: a
โ
3) Sequential
async function sequence() {
const output1 = await a();
const output2 = await b();
const output3 = await c();
return `sequence is done ${output1} ${output2} ${output3}`
}
sequence().then(console.log);
// -- output --
// ( 0์ดํ) Promise {<pending>}
// (8.1์ดํ) sequence is done a b c
โ
+ ์ ๋ค ํ๊บผ๋ฒ์ ํ๋ฉด...?
parallel().then(console.log);
sequence().then(console.log);
race().then(console.log);
// -- output --
// ( 0์ดํ) 01:12:56.033 Promise {<pending>}
// (0.1์ดํ) 01:12:56.139 race is done: a
// ( 5์ดํ) 01:13:01.033 parallel is done: a b c
// (8.1์ดํ) 01:13:04.166 sequence is done a b c
โ
์ถ์ฒ : Udemy - JavaScript: The Advanced Concept
https://www.udemy.com/course/advanced-javascript-concepts
'๊ณต๋ถ > priv | ๊ฐ์๋ ธํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- js
- ๋ถํธ์บ ํ
- HTML
- array
- KEYBOARD
- ๋ฐ๋๋ผ์ฝ๋ฉ ํ๊ธฐ
- ์ฝ๋ฉ๋ถํธ์บ ํ
- book
- stackoverflow
- VSC
- Stash
- GIT
- review
- css
- eslint
- eventlistener
- ๋ฐ๋๋ผ์ฝ๋ฉ
- string
- ์์ฑ์ํจ์
- DOM
- Total
- Today
- Yesterday