File tree Expand file tree Collapse file tree
09_Callbacks_promises_&_async-await Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Intro to Callbacks</ title >
7+ </ head >
8+ < body >
9+ < h1 > Callbacks</ h1 >
10+ < script src ="../JS/01_Intro_to_Callbacks.js "> </ script >
11+ </ body >
12+ </ html >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Intro to Promises</ title >
7+ </ head >
8+ < body >
9+ < h1 > Promises</ h1 >
10+ < script src ="../JS/03_Intro_to_promises.js "> </ script >
11+ </ body >
12+ </ html >
Original file line number Diff line number Diff line change 1+ // function loadscript(src, callback){
2+ // let script = document.createElement('script');
3+ // script.src = src
4+ // script.onload = callback()
5+ // document.body.appendChild(script)
6+ // }
7+ // src = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
8+ // function callback() {
9+ // alert("Script src loaded successfully")
10+ // }
11+ // loadscript(src, callback)
12+
13+
14+ // Handling Error
15+ function loadscript ( src , callback ) {
16+ let script = document . createElement ( 'script' ) ;
17+ script . src = src
18+ script . onload = function ( ) {
19+ console . log ( "Script src loaded successfully" )
20+ callback ( null , src )
21+ }
22+ script . onerror = function ( ) {
23+ console . log ( `Error loading script: ${ src } ` )
24+ callback ( new Error ( `Error loading script` ) )
25+ }
26+ document . body . appendChild ( script )
27+ }
28+ src = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
29+ function callback ( error , src ) {
30+ if ( error ) {
31+ console . log ( error )
32+ return
33+ }
34+ console . log ( src )
35+ }
36+ loadscript ( src , callback )
Original file line number Diff line number Diff line change 1+ //? Promise Syntax
2+ let promise1 = new Promise ( function ( resolve , reject ) {
3+ console . log ( "Promise - 1 state is Pending" )
4+ setTimeout ( function ( ) {
5+ resolve ( 'Promise - 1 State is resolved successfully' ) ;
6+ } , 2000 ) ;
7+ } )
8+
9+ let promise2 = new Promise ( function ( resolve , reject ) {
10+ console . log ( "Promise - 2 state is Pending" )
11+ setTimeout ( function ( ) {
12+ reject ( 'Promise - 2 State is rejected due to error' ) ;
13+ } , 2000 ) ;
14+ } )
15+
16+
17+ //? .then() ---> to get the result/value
18+ promise1 . then ( function ( value ) {
19+ console . log ( value ) ;
20+ } )
21+
22+ //? .catch() --> to get the error
23+ promise2 . catch ( function ( error ) {
24+ console . log ( error ) ;
25+ } )
26+
You can’t perform that action at this time.
0 commit comments