@@ -8,30 +8,28 @@ export class IncludeFragmentElement extends HTMLElement {
8
8
} )
9
9
}
10
10
11
- _fire ( name ) {
12
- setTimeout (
13
- function ( ) {
14
- const event = this . ownerDocument . createEvent ( 'Event' )
15
- event . initEvent ( name , false , false )
16
- this . dispatchEvent ( event )
17
- } . bind ( this ) ,
18
- 0
19
- )
11
+ _fire ( name , target ) {
12
+ setTimeout ( function ( ) {
13
+ const event = target . ownerDocument . createEvent ( 'Event' )
14
+ event . initEvent ( name , false , false )
15
+ target . dispatchEvent ( event )
16
+ } , 0 )
20
17
}
21
18
22
- async _handleData ( data ) {
23
- try {
24
- const html = await data
25
- const parentNode = this . parentNode
26
- if ( parentNode ) {
27
- this . insertAdjacentHTML ( 'afterend' , html )
28
- parentNode . removeChild ( this )
29
- }
30
- } catch ( err ) {
31
- // eslint-disable-next-line no-console
32
- console . log ( err )
33
- this . classList . add ( 'is-error' )
34
- }
19
+ _handleData ( data ) {
20
+ return data . then (
21
+ function ( html ) {
22
+ const parentNode = this . parentNode
23
+ if ( parentNode ) {
24
+ this . insertAdjacentHTML ( 'afterend' , html )
25
+ parentNode . removeChild ( this )
26
+ }
27
+ } . bind ( this ) ,
28
+ function ( err ) {
29
+ console . log ( err )
30
+ this . classList . add ( 'is-error' )
31
+ } . bind ( this )
32
+ )
35
33
}
36
34
37
35
static get observedAttributes ( ) {
@@ -115,30 +113,42 @@ export class IncludeFragmentElement extends HTMLElement {
115
113
} )
116
114
}
117
115
118
- async _load ( ) {
119
- const request = this . _request ( )
120
- this . _fire ( 'loadstart' )
121
- const response = await this . _fetch ( request )
122
-
123
- if ( response . status !== 200 ) {
124
- throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
125
- }
126
-
127
- const ct = response . headers . get ( 'Content-Type' )
128
- if ( ! ct || ! ct . match ( / ^ t e x t \/ h t m l / ) ) {
129
- throw new Error ( `Failed to load resource: expected text/html but was ${ ct } ` )
130
- }
131
-
132
- try {
133
- const data = await response . text ( )
134
- this . _fire ( 'load' )
135
- this . _fire ( 'loadend' )
136
- return data
137
- } catch ( error ) {
138
- this . _fire ( 'error' )
139
- this . _fire ( 'loadend' )
140
- throw error
141
- }
116
+ _load ( ) {
117
+ const self = this
118
+
119
+ return Promise . resolve ( )
120
+ . then ( function ( ) {
121
+ const request = self . _request ( )
122
+ self . _fire ( 'loadstart' , self )
123
+ return self . _fetch ( request )
124
+ } )
125
+ . then ( function ( response ) {
126
+ if ( response . status !== 200 ) {
127
+ throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
128
+ }
129
+
130
+ const ct = response . headers . get ( 'Content-Type' )
131
+ if ( ! ct || ! ct . match ( / ^ t e x t \/ h t m l / ) ) {
132
+ throw new Error ( `Failed to load resource: expected text/html but was ${ ct } ` )
133
+ }
134
+
135
+ return response
136
+ } )
137
+ . then ( function ( response ) {
138
+ return response . text ( )
139
+ } )
140
+ . then (
141
+ function ( data ) {
142
+ self . _fire ( 'load' , self )
143
+ self . _fire ( 'loadend' , self )
144
+ return data
145
+ } ,
146
+ function ( error ) {
147
+ self . _fire ( 'error' , self )
148
+ self . _fire ( 'loadend' , self )
149
+ throw error
150
+ }
151
+ )
142
152
}
143
153
144
154
_fetch ( request ) {
0 commit comments