Async... Await...
async function
μλ°μ€ν¬λ¦½νΈλ κΈ°λ³Έμ μΌλ‘ λκΈ°μ μΌλ‘ μ€νλλ μΈμ΄λ€.
μ΄λ μ½λκ° μμ±λ μμλλ‘ ν μ€μ© μ€νλλ©°, κ° λΌμΈμ μ½λκ° μλ£λκΈ° μ κΉμ§λ λ€μ λΌμΈμΌλ‘ λμ΄κ°μ§ μλλ€λ κ²μ μλ―Ένλ€.
μ΄λ¬ν λκΈ°μ μ€ν λͺ¨λΈμ μ½λμ νλ¦μ μμΈ‘νκΈ° μ½κ² νμ£Όμ§λ§, μ€λ 걸리λ μμ (μ: λ€νΈμν¬ μμ²)μ μ²λ¦¬ν λ λ¬Έμ κ° λ°μν μ μλ€.
async
μawait
μ μλ°μ€ν¬λ¦½νΈμμ λΉλκΈ° μμ μ λ νΈλ¦¬νκ² λ€λ£¨κΈ° μν΄ μ¬μ©λλ ν€μλλ€.
async
μ΄ ν€μλλ λΉλκΈ° ν¨μλ₯Ό μ μΈνλ λ° μ¬μ©λλ€.
async
ν¨μλPromise
λ₯Ό λ°ννλ€.μλ²μμ λ°μ΄ν°λ₯Ό κ°μ Έμ€λ λ±μ μκ°μ΄ 걸리λ μμ μ μννλ ν¨μκ° μμ λ asyncλ₯Ό μ μΈνκ³ μ¬μ©νλ€.
μλ°μ€ν¬λ¦½νΈμκ² μ΄ ν¨μλ₯Ό λΉλκΈ°μ μΌλ‘ μ²λ¦¬νλΌκ³ μλ €μ€λ€
async function fetchData() {
// APIμμ λ°μ΄ν° κ°μ Έμ€κΈ°
}
await
μ΄ ν€μλλ
async
ν¨μ λ΄μμPromise
κ° ν΄κ²°λ λκΉμ§ κΈ°λ€λ¦¬λ λ° μ¬μ©λλ€.ν¨μμ μ€νμ
Promise
κ° μ²λ¦¬λ λκΉμ§(μ΄νλκ±°λ κ±°λΆλ λκΉμ§) μΌμ μ€μ§νλ€.await
λ μ€μ§async
ν¨μ λ΄μμλ§ μ¬μ©ν μ μλ€.
async function fetchData() {
let data = await fetch('https://api.example.com/data');
let jsonData = await data.json();
return jsonData;
}
μμ
await
μ¬μ©νλ κ²½μ°
function resolveAfter2Seconds() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('resolved');
console.log('promise');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// Expected output:
// > "calling"
// > "promise"
// > "resolved"
}
asyncCall();
await
μ¬μ©νμ§ μλ κ²½μ°
function resolveAfter2Seconds() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('resolved');
console.log('promise');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = resolveAfter2Seconds();
console.log(result);
// Expected output:
// > "calling"
// > [object Promise] >
// "promise"`
asyncCall();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
Last updated
Was this helpful?