-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.js
101 lines (91 loc) · 1.62 KB
/
day12.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const errorIncoming = () =>{
throw Error("Error is here")
}
try{
console.log(errorIncoming())
}
catch(error){
console.log(error)
}
const divide= (num1,num2)=>{
if(num2==0) throw Error("Denominator is 0")
else return num1/num2
}
try{
console.log(divide(4,2))
}
catch(error){
console.log(error)
}
try{
console.log(divide(2,0))
}
catch(error){
console.log(error)
}
finally{
console.log("Work has been done")
}
class customError extends Error{
constructor(message){
super(message)
this.name="Custom Error"
}
}
const riskyFunction =()=>{
throw new customError("Batman is here")
}
try{
riskyFunction()
}
catch(error){
if(error instanceof customError){
console.log(error)
}
}
const nameCheck=(name)=>{
if(name==='') throw Error("Please give a valid name")
else console.log(name);
}
try{
console.log(nameCheck(""))
}
catch(error){
console.log(error)
}
const promise1 = new Promise((resolve,reject)=>{
const data=true;
if(data) resolve("2")
else reject("There is a error")
})
promise1.then((res)=>{
console.log(res)
})
.catch((error)=>{
console.log(error)
})
async function consumePromise(){
try{
const data=promise1()
console.log(data)
}
catch(error){
console.log(error)
}
}
fetch('https://randomuser.me/api/').then((res)=>{
console.log(res)
})
.catch((error)=>{
console.log(error)
})
async function consumeFetch(){
try{
const data=await fetch('https://randomuser.me/api/')
console.log(data)
}
catch(error){
console.log(error)
}
}
consumeFetch()