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 | 31 |
Tags
- 가상메모리
- 재귀
- 백준
- 코딩테스트
- BFS
- 수학
- level1
- BOJ
- 스택
- 딕셔너리
- 파이썬
- level2
- N과M
- level0
- MYSQL
- 구현
- 그리디
- 힙
- 다이나믹 프로그래밍
- level3
- 브루트포스
- dfs
- DP
- 다익스트라
- programmers
- 프로그래머스
- 가상메모리 관리
- 운영체제
- python
- 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 |