-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromises.html
More file actions
119 lines (109 loc) · 3.66 KB
/
Copy pathPromises.html
File metadata and controls
119 lines (109 loc) · 3.66 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Promises in JS</title>
</head>
<body style="background-color: #212121; color: #fff;">
<h2>Asyncronious Programing</h2>
<script>
const promise_1= new Promise(function(resolve , reject){ // 2 parts of promise resolve and reject
// Do and async call
// DB calls , cryptography , network
setTimeout(function(){
console.log('Async task is complete');
resolve() // connect the .then with resolve then its run
},1000)
})
promise_1.then(function(){ // consumption of promise
console.log('Promoise consume');
})
// or
new Promise(function(resolve , reject){
setTimeout(function(){
console.log('Async task 2');
resolve()
},1000)
}).then(function(){
console.log("Async 2 is resolved");
})
const promise_3 = new Promise(function (resolve , reject){
setTimeout(function(){
resolve({
userName: "chai" ,
email: 'chai@gmail.com',
age : 21
})
},1000)
})
promise_3.then(function(user){
console.log(user);
})
const promise_4 = new Promise(function(resolve, reject){
setTimeout(function(){
let erorr = false
if(!erorr){
resolve({userName : "Shifa", password: 321})
}else{
reject('ERORR : Somethig went wrong')
}
},1000)
})
promise_4.then((user)=>{ //how to avoide callBack hell
console.log(user);
return user.userName
})
.then((userName)=>{ // work in database connection
console.log(userName);
})
.catch(function(erorr){
console.log(erorr);
}).finally(() =>{
console.log('The promise is either resoved or rejected');
})
const promise_5 = new Promise(function(resolve, reject) {
setTimeout(function(){
let erorr = true // if erorr acour this is an other way to solve a erorr
if(!erorr){
resolve({userName: "JavaScript" , password:'432'})
}else{
reject('ERORR: JS went wrong')
}
},1000)
});
async function consumePromiseFive (){
try {
const response= await promise_5
console.log(response);
}catch (erorr){
console.log(erorr);
}
}
consumePromiseFive ();
// ****************************************
// async function getallUsers() {
// try{
// const response = await fetch('https://api.github.com/users/hiteshchoudhary')
// const data = await response.json ()
// console.log(data);
// }
// catch (erorr){
// console.log("E:" , erorr);
// }
// }
// getallUsers()
// ************** or ********************
fetch('https://api.github.com/users/hiteshchoudhary')
.then((response) => {
return response.json()
})
.then((data) => {
console.log(data);
})
.catch((erorr) => {
console.log(erorr);
})
</script>
</body>
</html>