function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function helloAsync() {
await delay(3000);
return "hello sync";
}
async function main() {
const res = await helloAsync();
console.log(res);
}
main();
async는 promise를 반환한다. await와 함께 작성되어 있는 코드는 동기적으로 시행된다. 즉 delay(3000)이 다 시행될 때까지 아래의 코드는 시해오디지 않는다.