HTML & CSS & JS/JS

[JS] async 와 await

가자미 2021. 3. 17. 20:05

async : 함수 앞에 붙이면, 반환 값이 반드시 promise형이 된다.

await : async에만 사용할 수 있음. 프로미스 인스턴스가 완료될 때까지 대기. then과 비슷

//async & await
function sleep(ms){
    return new Promise(resolve=> setTimeout(resolve,ms));
}
async function process(){
    console.log('hello');
    await sleep(1000);
    console.log('nice to meet u');
    return true;//async의 반환형은 promise(then catch사용 가능)
}
process();