1. Timeouts/Intervals
setTimeout(() => { console.log('timeout callback'); }, 1000);
console.log('second console log');
// 예상 결과
// second console log
// timeout callback
2. Promise
let myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise Success!");
}, 1000);
});
myPromise.then((message) => {
console.log("message? " + message);
});
3. Async/Await
let myPromise = async () => {
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise Success!");
}, 1000);
});
let result = await promise; // promise 반환 될 때까지 기다림
console.log(result);
console.log('after new promise');
};
console.log('first console log');
myPromise();
console.log('second console log');
모던 자바스크립트 DeepDive
JavaScript Async/Await Tutorial – Learn Callbacks, Promises, and Async/Await in JS by Making Ice Cream
Asynchronous JavaScript
Promise - MDN
async와 await를 사용하여 비동기 프로그래밍을 쉽게 만들기
NEXT. JavaScript EventLoop