#author("2021-03-01T18:13:06+09:00","default:ryuichi","ryuichi")
#author("2021-03-01T18:13:37+09:00","default:ryuichi","ryuichi")
* Promiseからasync-awaitへの変換 [#c9b689c4]
** Promise [#m5fcabaf]
function restaurantCustomer() {
return getCustomer()
.then(customer => {
return getOrder(customer);
}).then(order => {
return prepareFood(order);
}).then(meal => {
return serveFood(meal);
}).then(food => {
return eatFood(food, customer);
}).catch(showError);
** async/await [#j5997050]
async function restaurantCustomer() {
try{
let customer = await getCustomer();
let order = await getOrder(customer);
let meal = await prepareFood(order);
let food = await serveFood(meal);
return await eatFood(food);
} catch(e) {
showError(e);
}
}
** 参考 [#ke583b85]
https://afteracademy.com/blog/migrating-from-promise-chains-to-async-await