-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsyncWait.html
61 lines (59 loc) · 2.01 KB
/
AsyncWait.html
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
<html>
<head>
<title>Async Wait Sample | Js</title>
<script type="text/javascript">
const _process =async ()=>{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
reject({
errorCode : 500,
errorMessage : 'Unable to process the request'
});
//resolve('Processed successfully');
},400)
})
}
const asyncProcess = async ()=>{
console.log('asyncProcess()');
// let response =await _process();
let response = _process();
// handle response
response.then((resolve)=>{
console.log('Success....');
}).catch((reject)=>{
console.log('Failure.... '+JSON.stringify(reject));
});
console.log('Process Finished');
}
asyncProcess();
// approach one
const loadUsers =async ()=>{
let response = await fetch('https://jsonplaceholder.typicode.com/users')
let users =await response.json();
// remap with specific fields
users = users.map(u=>{
return '=> '+u.name +' ,'+u.email +' ,'+ u.address.street
})
console.log(users);
}
const loadUsers1 = async ()=>{
let promise1 = fetch('https://jsonplaceholder.typicode.com/users')
promise1.then((promise2)=>{
const customPromise = async ()=>{
let usrs = await promise2.json()
console.log(usrs.map(u=>{
return '# '+u.id +' ,Name: '+u.name
}));
}
customPromise();
}).catch((error)=>{
console.log('Error Found ',error);
})
}
loadUsers1();
//loadUsers();
</script>
</head>
<body>
</body>
</html>