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

release 0 #13

Open
wants to merge 2 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
28 changes: 15 additions & 13 deletions beli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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);
function beli(uang, obj){
return new Promise((resolve,reject)=>{
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}`);
resolve(kembalian)
}else{
console.log(`uang gk cukup nih buat beli ${obj.item} Kurang Rp.${Math.abs(kembalian)}`);
reject(uang)
}
}, obj.waktu);
})
}

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

let cumi ={}
cumi.item ='cumi';
cumi.harga = 10000;
cumi.waktu = 500;
let hoisem ={}
hoisem.item ='hoisem';
hoisem.harga = 5000;
hoisem.waktu = 500;
let abalone ={}
abalone.item ='abalone';
abalone.harga = 3000;
abalone.waktu = 500;
let kerang ={}
kerang.item ='kerang';
kerang.harga = 2000;
kerang.waktu = 500;
let caviar ={}
caviar.item ='caviar';
caviar.harga = 1000;
caviar.waktu = 500;

// beli(15000,cumi).then((change1)=>{
// beli(change1,hoisem).then((change2)=>{
// beli(change2,abalone).then((change3)=>{
// beli(change3,kerang).then((change4)=>{
// beli(change4,caviar).then((change5)=>{
// console.log(`Sisa uang Rp.${change5}`)
// }).catch((change5)=>console.log(`Sisa uang Rp.${change5}`));
// }).catch((change4)=>console.log(`Sisa uang Rp.${change4}`));
// }).catch((change3)=>console.log(`Sisa uang Rp.${change3}`));
// }).catch((change2)=>console.log(`Sisa uang Rp.${change2}`));
// }).catch((change1)=>console.log(`Sisa uang Rp.${change1}`));

beli(500,cumi)
.then((change)=> beli(change,hoisem))
.then((change)=> beli(change,abalone))
.then((change)=> beli(change,kerang))
.then((change)=> beli(change,caviar))
.catch((change)=>console.log(`Sisa uang Rp.${change}`));