Skip to content

Commit a887e13

Browse files
authored
Merge pull request #83 from lgarron/move-try
Fix the location of `try` in new async code.
2 parents e1d2167 + 9f70357 commit a887e13

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,18 @@ export default class IncludeFragmentElement extends HTMLElement {
178178
// We mimic the same event order as <img>, including the spec
179179
// which states events must be dispatched after "queue a task".
180180
// https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
181-
182-
await this.#task(['loadstart'])
183-
const response = await this.fetch(this.request())
184-
if (response.status !== 200) {
185-
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
186-
}
187-
const ct = response.headers.get('Content-Type')
188-
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
189-
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
190-
}
191-
const data = await response.text()
192-
193181
try {
182+
await this.#task(['loadstart'])
183+
const response = await this.fetch(this.request())
184+
if (response.status !== 200) {
185+
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
186+
}
187+
const ct = response.headers.get('Content-Type')
188+
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
189+
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
190+
}
191+
const data = await response.text()
192+
194193
// Dispatch `load` and `loadend` async to allow
195194
// the `load()` promise to resolve _before_ these
196195
// events are fired.

0 commit comments

Comments
 (0)