Chaining Promise using async/await

Run Settings
LanguageJavascript
Language Version
Run Command
//melakukan promise berantai bila menggunakan async/await const state = { isCoffeeMakerReady: true, seedStocks: { arabica: 250, robusta: 60, liberica: 80 } } const getSeeds = (type, miligrams) => { return new Promise((resolve, reject) => { if(state.seedStocks[type] >= miligrams) { state.seedStocks[type] -= miligrams; resolve("Biji kopi didapatkan!") }else{ reject("Maaf stock kopi habis!") } }); } const makeCoffee = seeds => { return new Promise((resolve, reject) => { if(state.isCoffeeMakerReady){ resolve("kopi Berhasil dibuat") } else { reject("Maaf mesin kopi sedang tidak dapat digunakan") } }) } const servingToTable = coffee => { return new Promise(resolve => { resolve("Pesanan kopi sudah selesai!") }); } async function reserveACoffee(type, miligrams){ try{ const seeds = await getSeeds(type, miligrams); const coffee = await makeCoffee(seeds); const result = await servingToTable(coffee); console.log (result); }catch (rejectionReason) { console.log(rejectionReason); } } reserveACoffee("robusta", 30); reserveACoffee("liberica", 80);
Editor Settings
Theme
Key bindings
Full width
Lines