업데이트 된 버전을 사용하는 await것보다 .then().
await약속이 해결 될 때까지 실행을 중지합니다. 사용 .then()하는 것과 달리 await약속을 반환하는 다양한 작업을 수행하면서 값을 유지할 수 있으며 실행은 다음 줄로 계속 진행됩니다 ( '직접 스타일'이라고 함). 을 보는 것이 훨씬 좋습니다 .then(). 다른 곳 과 비교할 때 나머지 JavaScript와 일치하기 때문 입니다.
// Example function that returns a Promise that will resolve after 2 seconds
var getGenres = function() {
return new Promise(function(resolve) {
setTimeout(function(){
resolve(['comedy', 'drama', 'action'])
}, 2000);
});
}
// We start an 'async' function to use the 'await' keyword
(async function(){
var result = await getGenres()
console.log('Woo done!', result)
// But the best part is, we can just keep awaiting different stuff, without ugly .then()s
var somethingElse = await getSomethingElse()
var moreThings = await getMoreThings()
})()
Await는 모든 현재 브라우저 및 노드에서 지원됩니다.
resolve([genre1, genre2, ...]);promise 구현 내에서 해야한다고 말하고 싶을 것입니다.