가자미의 개발이야기

[JS] async 와 await 본문

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();

'HTML & CSS & JS > JS' 카테고리의 다른 글

[JS] promise.all 과 promise.race  (0) 2021.03.17
[JS] 많은 콜백함수를 정리하는 promise  (0) 2021.03.17
[JS] 비동기 작업을 다루는 setTimeout()  (0) 2021.03.17
[JS] spread, rest  (0) 2021.03.17
[JS] 비구조할당  (0) 2021.03.17