Skip to content

Instantly share code, notes, and snippets.

@xodhx4
Created April 2, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xodhx4/194fc29cfda9cff846e56ac394860edb to your computer and use it in GitHub Desktop.
Save xodhx4/194fc29cfda9cff846e56ac394860edb to your computer and use it in GitHub Desktop.
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
function resolveAfter3Seconds() { // 3초 동안 대기하게 함
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved1');
}, 3000);
});
}
function resolveAfter2Seconds() { // 2초 동안 대기하게 함
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved2');
}, 2000);
});
}
async function printAsync1() {
console.log('calling1');
const result = await resolveAfter3Seconds();
return result;
}
async function printAsync2() {
console.log('calling2');
const result = await resolveAfter2Seconds();
return result;
}
const result1 = await printAsync1(); // 실제로는 불가능함
console.log(result1);
const result2 = await printAsync2(); // 실제로는 불가능함
console.log(result2);
// > "calling1"
// > "resolved1"
// > "calling2"
// > "resolved2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment