-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.js
More file actions
47 lines (33 loc) · 1.17 KB
/
Copy pathTask1.js
File metadata and controls
47 lines (33 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Tugas1
// Buatlah sambungan program menggunakan then catch dan juga try catch untuk mengecek hari kerja dari kode Asynchronous dibawah dan jelaskan penggunaan then catch dan try catch dengan memberikan komentar di bawahnya:
const cekHariKerja = (day) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const dataDay = ['senin', 'selasa', 'rabu', 'kamis', 'jumat']
let cek = dataDay.find((item) => {
return item === day
})
if (cek) {
resolve(cek)
} else {
reject(new Error('Hari ini bukan hari kerja'))
}
}, 3000)
})
}
cekHariKerja('minggu')
.then((result) => console.log(result))
.catch((error) => console.log(error))
//then adalah mengeksekusi kondisi yang true
//catch adalah mengeksekusi kondisi yang false
const getcekHariKerja = async() => {
try {
let result = await cekHariKerja('minggu')
console.log(result)
} catch (error) {
console.log(error.message)
}
}
getcekHariKerja()
// try adalah untuk menguji apakah kode terjadi kesalahan
// cacth menangani eror yang ditangkep oleh try