Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Tags
- level3
- 프로그래머스
- DP
- 다익스트라
- MYSQL
- 운영체제
- python
- 브루트포스
- BOJ
- N과M
- 그리디
- dfs
- 다이나믹 프로그래밍
- 딕셔너리
- level0
- level1
- 재귀
- 구현
- 백준
- programmers
- 파이썬
- 가상메모리 관리
- 스택
- 수학
- 가상메모리
- BFS
- 코딩테스트
- level2
- dict
- 힙
Archives
- Today
- Total
동캄의 코딩도장
JS [async] 본문
// async & await
// clear style of using promise :)
// 1. async
//promise 사용
// function fetchUser(){
// return new Promise((resolve,reject)=>{
// resolve('ellie');
// });
// }
// const user =fetchUser();
// user.then(console.log);
// console.log(user);
// async 사용
async function fetchUser(){
return 'ellie';
}
const user=fetchUser();
user.then(console.log);
// 2, await
function delay(ms){
return new Promise(resolve=>setTimeout(resolve,ms));
}
async function getApple(){
await delay(2000);
return 'apple';
}
async function getBanana(){
await delay(3000);
return 'banana';
}
//promise 사용
// function pickFruits(){
// return getApple()
// .then(apple =>{
// return getBanana().then(banana=>`${apple}+${banana}`);
// })
// }
async function pickFruits(){
try{
const apple = await getApple();
const banana = await getBanana();
return `${apple}+${banana}`;
}
catch{
}
}
pickFruits().then(console.log);
// useful promise APIs 병렬처리
function pickAllFruits(){
return Promise.all([getApple(),getBanana()])
.then(fruits=>fruits.join('+'));
}
pickAllFruits().then(console.log);
function pickOnlyOne(){
return Promise.race([getApple(),getBanana()]);
}
pickOnlyOne().then(console.log);
'front > HTML&CSS&JS' 카테고리의 다른 글
JQuery (0) | 2023.03.08 |
---|---|
JS [콘솔 사용 tip] (0) | 2022.01.27 |
JS [callback-to-promise 예제] (0) | 2022.01.25 |
JS [promise] (0) | 2022.01.25 |
JS [call back] (0) | 2022.01.25 |