1
+ const promiseOne = new Promise ( function ( resolve , reject ) {
2
+ //Do an async task
3
+ // DB calls, cryptography, network
4
+ setTimeout ( function ( ) {
5
+ console . log ( 'Async task is compelete' ) ;
6
+ resolve ( )
7
+ } , 1000 )
8
+ } )
9
+
10
+ promiseOne . then ( function ( ) {
11
+ console . log ( "Promise consumed" ) ;
12
+ } )
13
+
14
+ new Promise ( function ( resolve , reject ) {
15
+ setTimeout ( function ( ) {
16
+ console . log ( "Async task 2" ) ;
17
+ resolve ( )
18
+ } , 1000 )
19
+
20
+ } ) . then ( function ( ) {
21
+ console . log ( "Async 2 resolved" ) ;
22
+ } )
23
+
24
+ const promiseThree = new Promise ( function ( resolve , reject ) {
25
+ setTimeout ( function ( ) {
26
+ resolve ( { username :
"Chai" , email :
"[email protected] " } )
27
+ } , 1000 )
28
+ } )
29
+
30
+ promiseThree . then ( function ( user ) {
31
+ console . log ( user ) ;
32
+ } )
33
+
34
+ const promiseFour = new Promise ( function ( resolve , reject ) {
35
+ setTimeout ( function ( ) {
36
+ let error = true
37
+ if ( ! error ) {
38
+ resolve ( { username : "hitesh" , password : "123" } )
39
+ } else {
40
+ reject ( 'ERROR: Something went wrong' )
41
+ }
42
+ } , 1000 )
43
+ } )
44
+
45
+ promiseFour
46
+ . then ( ( user ) => {
47
+ console . log ( user ) ;
48
+ return user . username
49
+ } ) . then ( ( username ) => {
50
+ console . log ( username ) ;
51
+ } ) . catch ( function ( error ) {
52
+ console . log ( error ) ;
53
+ } ) . finally ( ( ) => console . log ( "The promise is either resolved or rejected" ) )
54
+
55
+
56
+
57
+ const promiseFive = new Promise ( function ( resolve , reject ) {
58
+ setTimeout ( function ( ) {
59
+ let error = true
60
+ if ( ! error ) {
61
+ resolve ( { username : "javascript" , password : "123" } )
62
+ } else {
63
+ reject ( 'ERROR: JS went wrong' )
64
+ }
65
+ } , 1000 )
66
+ } ) ;
67
+
68
+ async function consumePromiseFive ( ) {
69
+ try {
70
+ const response = await promiseFive
71
+ console . log ( response ) ;
72
+ } catch ( error ) {
73
+ console . log ( error ) ;
74
+ }
75
+ }
76
+
77
+ consumePromiseFive ( )
78
+
79
+ // async function getAllUsers(){
80
+ // try {
81
+ // const response = await fetch('https://jsonplaceholder.typicode.com/users')
82
+
83
+ // const data = await response.json()
84
+ // console.log(data);
85
+ // } catch (error) {
86
+ // console.log("E: ", error);
87
+ // }
88
+ // }
89
+
90
+ //getAllUsers()
91
+
92
+ fetch ( 'https://api.github.com/users/hiteshchoudhary' )
93
+ . then ( ( response ) => {
94
+ return response . json ( )
95
+ } )
96
+ . then ( ( data ) => {
97
+ console . log ( data ) ;
98
+ } )
99
+ . catch ( ( error ) => console . log ( error ) )
100
+
101
+ // promise.all
102
+ // yes this is also available, kuch reading aap b kro
0 commit comments