Diary
[개발일기] promise를 사용하여 sleep함수 만들기
브라더 코드
2023. 7. 10. 09:23
(2023.6.17) js에는 일정시간 뒤에 함수를 실행하는 기능이 없다.
그래서 promise로 만들어야 한다. 일명 sleep함수.
const sleep = time => new Promise((res,rej) => setTimeout(res, time));
// 1. then
sleep(2000).then(() => console.log('1. 콜백함수 실행'));
// 2. async await
await sleep(1000);
console.log('2. 콜백함수 실행');
// 콘솔 출력은 아래와 같다.
// 2. 콜백함수 실행
// 1. 콜백함수 실행