JavaScript async-await

김혜림·2023년 5월 17일
0

자바스크립트에서 동기적으로 함수를 실행하기 위해서 async, await 키워드를 사용합니다.

async 키워드를 사용해서 메소드를 선언하면, 메소드 내의 코드가 동기적으로 해석되도록 할 수 있습니다.

const printString = function (string) {
	return new Promise( (~)=>{~} )
}

const printAll = async()=>{
	await printString('A');
	await printString('B');
	await printString('C');
}

위의 예제에서는, Promise객체를 return 하는 printString 메소드를 구현하고, printAll 이라는 메소드에서 printString 메소드를 실행합니다.

await printString('A') 코드를 읽었을 때, await 키워드 뒤에서 실행한 메소드 printString('A')가 실행이 완료될 때까지 뒤의 코드

	await printString('B');
	await printString('C');

는 실행되지 않습니다. printString('A') 가 실행이 완료되어 리턴값을 반환하면 다음 줄인 await printString('B')가 실행됩니다.

profile
공부하는 혜림이😀

0개의 댓글