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

DONE #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

DONE #19

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
25 changes: 15 additions & 10 deletions beli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
"use strict"
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);
return new Promise(function(resolve, reject) {
setTimeout(function(){
let kembalian = uang - obj.harga
if (kembalian > 0) {
console.log(`Saya sudah membeli ${obj.item} uang kembaliannya ${kembalian}`);
resolve(kembalian)

}else{
console.log(`uang gk cukup nih buat beli ${obj.item} kembaliannya cuma ${kembalian}`);
reject(0)
}
}, obj.waktu);
});
}


module.exports = beli;
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict"
const beli=require('./beli.js')
var uang=10000
var obj=[{
item:'kopi',
harga: 2000,
waktu : 2000
},
{
item:'permen',
harga: 500,
waktu: 2000
},
{
item:'teh',
harga: 5000,
waktu: 2000
},
{
item:'ciki',
harga: 3500,
waktu: 2000
},
{
item:'kue',
harga: 1000,
waktu: 2000
}
]

beli(uang,obj[0]).then(sisa=>{
beli(sisa,obj[1]).then(sisa=>{
beli(sisa,obj[2]).then(sisa=>{
beli(sisa,obj[3]).then(sisa=>{
beli(sisa,obj[4]).then(sisa=>{
}).catch(err=>{
console.log(err);
})
}).catch(err=>{
console.log(err);
})
}).catch(err=>{
console.log(err);
})
}).catch(err=>{
console.log(err);
})
}).catch(err=>{
console.log(err);
})