Promise를 더 쉽고 가독성 좋게 다루는 기능

function hello(){
	return "hello";
}

async function helloAsync(){  // async를 써주게 되면 해당 함수는 Promise를 반환하게 된다.
	return "hello Async";  //resolve 가 된거라 생각하면 
}

console.log(hello());
console.log(helloAsync());

Untitled


async function helloAsync(){  // async를 써주게 되면 Promise를 반환하게 된다.
	return "hello Async";
}

helloAsync().then((res) => {
	console.log(res);
});

Untitled

function delay (ms){
	return new Promise((resolve)=>{
		setTimeout(resolve, ms);
	});
}

async function helloAsync(){  // async를 써주게 되면 Promise를 반환하게 된다.
	return delay(3000).then(() => {
		return "hello Async";
	});
}

-------------------위에 helloAsync를 바꾸면?-------------------
async function helloAsync(){  
	await delay(3000); // await 붙은 함수는 마치 동기처럼 작동
	return "hello async";
}

async function main(){
	const res = await helloAsync();
	console.log(res);
}

main()

Untitled