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

WikaSilo #14

Open
wants to merge 3 commits 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
20 changes: 11 additions & 9 deletions beli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
function beli(uang, obj, cb){
function beli(uang, obj) {
console.log(`Saya pergi membeli ${obj.item}`)
setTimeout(function(){
return new Promise((resolve, reject) =>{
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)
if (kembalian >= 0) {
setTimeout(function(){
console.log(`Saya sudah membeli ${obj.item} uang kembaliannya ${kembalian}`)
resolve(kembalian)
}, obj.waktu)
} else {
console.log(`uang gk cukup nih buat beli ${obj.item}, cuma ada ${uang}`);
reject(uang)
}
}, obj.waktu);
})
}

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

let uang = 7000

let buku = {}
buku.item = 'Buku'
buku.harga = 2000
buku.waktu = 1000

let pulpen = {}
pulpen.item = 'Pulpen'
pulpen.harga = 1000
pulpen.waktu = 1500

let makanan = {}
makanan.item = 'Makanan'
makanan.harga = 5000
makanan.waktu = 2000

let minuman = {}
minuman.item = 'Minuman'
minuman.harga = 4000
minuman.waktu = 2000

let susu = {}
susu.item = 'Susu'
susu.harga = 7000
susu.waktu = 2000

beli(uang, buku)
.then(function (kembalian) {
return beli(kembalian, pulpen)
}).then(function (kembalian) {
return beli(kembalian, makanan)
}).then(function (kembalian) {
return beli(kembalian, minuman)
}).then(function (kembalian) {
return beli(kembalian, susu)
}).catch(reject =>{})

// beli(uang, buku)
// .then(function (kembalian) {
// beli(kembalian, pulpen)
// .then(function (kembalian) {
// beli(kembalian, makanan)
// .then(function (kembalian) {
// beli(kembalian, minuman)
// .then(function (kembalian) {
// beli(kembalian, susu)
// }).catch(reject =>{})
// }).catch(reject =>{})
// }).catch(reject =>{})
// }).catch(reject =>{})