@@ -56,39 +56,66 @@ let aPromise = new Promise((resolve, reject) => {
5656 console . log ( "hey! I'm from a promise outside the setTimeout() " ) ; // snyc execution
5757 setTimeout ( ( ) => {
5858 console . log ( "hey! I'm from a promise inside the setTimeout()" ) ; // async execution
59- resolve ( "aPromise is resolved!" ) ;
59+ // resolve("aPromise is resolved!");
60+ reject ( new Error ( "promise got rejected with ERROR!" ) ) ;
6061 console . log ( aPromise ) ;
6162 } , 2000 ) ;
6263} ) ;
64+ // aPromise.then((value) => {
65+ // console.log(value + " I'm After .then");
66+ // });
67+ aPromise . catch ( ( error ) => {
68+ console . log ( error + " this error occured sir!" ) ;
69+ } ) ;
6370
6471// ChatGPT Promise Example:
65- // // Creating a Promise that represents an HTTP request
66- // const fetchData = (url) => {
72+ // Creating a Promise that represents an HTTP request
73+ const fetchData = ( url ) => {
74+ return new Promise ( ( resolve , reject ) => {
75+ fetch ( url )
76+ . then ( ( response ) => {
77+ if ( response . ok ) {
78+ return response . json ( ) ;
79+ } else {
80+ throw new Error ( "Network response was not ok." ) ;
81+ }
82+ } )
83+ . then ( ( data ) => {
84+ resolve ( data ) ; // Fulfill the Promise with the fetched data
85+ } )
86+ . catch ( ( error ) => {
87+ reject ( error ) ; // Reject the Promise with the error
88+ } ) ;
89+ } ) ;
90+ } ;
91+ // Using the fetchData Promise
92+ const apiUrl = "https://jsonplaceholder.typicode.com/posts/1" ;
93+
94+ fetchData ( apiUrl )
95+ . then ( ( data ) => {
96+ console . log ( "Fetched data:" , data ) ;
97+ } )
98+ . catch ( ( error ) => {
99+ console . error ( "Error:" , error ) ;
100+ } ) ;
101+
102+ // Promises Chaining:
103+
104+ // const loadScript2 = (src) => {
67105// return new Promise((resolve, reject) => {
68- // fetch(url)
69- // .then(response => {
70- // if (response.ok) {
71- // return response.json();
72- // } else {
73- // throw new Error('Network response was not ok.');
74- // }
75- // })
76- // .then(data => {
77- // resolve(data); // Fulfill the Promise with the fetched data
78- // })
79- // .catch(error => {
80- // reject(error); // Reject the Promise with the error
81- // });
106+ // let script2 = document.createElement("script");
107+ // script2.src = src;
108+ // document.body.appendChild(script2);
109+ // if (script2.onload == true) {
110+ // console.log(`Script with ${src} is inserted successfully!`);
111+ // } else {
112+ // throw new Error("Sry unexpectedly script failed to load");
113+ // }
114+ // }).then(() => {
115+ // resolve(1);
82116// });
83117// };
84118
85- // // Using the fetchData Promise
86- // const apiUrl = 'https://jsonplaceholder.typicode.com/posts/1';
87-
88- // fetchData(apiUrl)
89- // .then(data => {
90- // console.log('Fetched data:', data);
91- // })
92- // .catch(error => {
93- // console.error('Error:', error);
94- // });
119+ // loadScript2(
120+ // "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
121+ // );
0 commit comments