Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promise belanja #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions beli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
function beli(uang, obj, cb){
console.log(`Saya pergi membeli ${obj.item}`)
setTimeout(function(){
let kembalian = uang - obj.harga
if (kembalian > 0) {
console.log(`Saya sudah membeli ${obj.item} uang kembaliannya ${kembalian}`);
cb(kembalian)
}else{
console.log(`uang gk cukup nih buat beli ${obj.item} kembaliannya cuma ${kembalian}`);
cb(0)
}
}, obj.waktu);
class Belanja {
static beli(uang, obj) {
return new Promise((resolve, reject)=>{
console.log(`Saya pergi membeli ${obj.item}`)
setTimeout(()=>{
let kembalian = uang - obj.harga
if(kembalian > obj.harga) {
console.log(`Saya sudah membeli ${obj.item} uang kembaliannya ${kembalian}`)
resolve(kembalian)
} else {
reject(`uang gak cukup nih buat beli ${obj.item} kembaliannya cuma ${kembalian}`)
}
}, obj.waktu)
})
}
}

module.exports = beli;
module.exports = Belanja;
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Belanja = require('./beli.js')

let indomie = {item: 'indomie', harga: 1500, time: 500}
let sarden = {item: 'sarden', harga: 7000, waktu: 500}
let gula = {item : 'gula', harga: 4000, waktu: 500}
let garam = {item : 'garam', harga: 3000, waktu: 500}
let royco = {item : 'royco', harga: 2000, waktu: 500}

Belanja.beli(10000, indomie)
.then(kembalian => {Belanja.beli(kembalian, sarden)
.then(kembalian => {Belanja.beli(kembalian, gula)
.then(kembalian => {Belanja.beli(kembalian, garam)
.then(kembalian => {Belanja.beli(kembalian, royco)})})})
.catch(err => {console.log(err)})})