Skip to content

Commit 2fb836c

Browse files
committed
Switch Promise chaining to async-await syntax.
This will make future PRs like #81 easier to author and review.
1 parent 7f8fbf0 commit 2fb836c

File tree

1 file changed

+52
-57
lines changed

1 file changed

+52
-57
lines changed

src/index.ts

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,28 @@ export default class IncludeFragmentElement extends HTMLElement {
133133
}
134134
)
135135

136-
#handleData(): Promise<void> {
136+
async #handleData(): Promise<void> {
137137
if (this.#busy) return Promise.resolve()
138138
this.#busy = true
139-
140-
this.#observer.unobserve(this)
141-
return this.#getData().then(
142-
(html: string) => {
143-
const template = document.createElement('template')
144-
// eslint-disable-next-line github/no-inner-html
145-
template.innerHTML = html
146-
const fragment = document.importNode(template.content, true)
147-
const canceled = !this.dispatchEvent(
148-
new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}})
149-
)
150-
if (canceled) return
151-
this.replaceWith(fragment)
152-
this.dispatchEvent(new CustomEvent('include-fragment-replaced'))
153-
},
154-
() => {
155-
this.classList.add('is-error')
139+
try {
140+
this.#observer.unobserve(this)
141+
const html = await this.#getData()
142+
143+
const template = document.createElement('template')
144+
// eslint-disable-next-line github/no-inner-html
145+
template.innerHTML = html
146+
const fragment = document.importNode(template.content, true)
147+
const canceled = !this.dispatchEvent(
148+
new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}})
149+
)
150+
if (canceled) {
151+
return
156152
}
157-
)
153+
this.replaceWith(fragment)
154+
this.dispatchEvent(new CustomEvent('include-fragment-replaced'))
155+
} catch (_) {
156+
this.classList.add('is-error')
157+
}
158158
}
159159

160160
#getData(): Promise<string> {
@@ -173,47 +173,42 @@ export default class IncludeFragmentElement extends HTMLElement {
173173
}
174174
}
175175

176-
#fetchDataWithEvents(): Promise<string> {
176+
async #fetchDataWithEvents(): Promise<string> {
177177
// We mimic the same event order as <img>, including the spec
178178
// which states events must be dispatched after "queue a task".
179179
// https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
180-
return task()
181-
.then(() => {
182-
this.dispatchEvent(new Event('loadstart'))
183-
return this.fetch(this.request())
184-
})
185-
.then(response => {
186-
if (response.status !== 200) {
187-
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
188-
}
189-
const ct = response.headers.get('Content-Type')
190-
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
191-
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
192-
}
193-
return response.text()
194-
})
195-
.then(
196-
data => {
197-
// Dispatch `load` and `loadend` async to allow
198-
// the `load()` promise to resolve _before_ these
199-
// events are fired.
200-
task().then(() => {
201-
this.dispatchEvent(new Event('load'))
202-
this.dispatchEvent(new Event('loadend'))
203-
})
204-
return data
205-
},
206-
error => {
207-
// Dispatch `error` and `loadend` async to allow
208-
// the `load()` promise to resolve _before_ these
209-
// events are fired.
210-
task().then(() => {
211-
this.dispatchEvent(new Event('error'))
212-
this.dispatchEvent(new Event('loadend'))
213-
})
214-
throw error
215-
}
216-
)
180+
181+
try {
182+
await task()
183+
184+
this.dispatchEvent(new Event('loadstart'))
185+
const response = await this.fetch(this.request())
186+
187+
if (response.status !== 200) {
188+
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
189+
}
190+
const ct = response.headers.get('Content-Type')
191+
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
192+
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
193+
}
194+
const data = await response.text()
195+
196+
// Dispatch `load` and `loadend` async to allow
197+
// the `load()` promise to resolve _before_ these
198+
// events are fired.
199+
await task()
200+
this.dispatchEvent(new Event('load'))
201+
this.dispatchEvent(new Event('loadend'))
202+
return data
203+
} catch (error) {
204+
// Dispatch `error` and `loadend` async to allow
205+
// the `load()` promise to resolve _before_ these
206+
// events are fired.
207+
await task()
208+
this.dispatchEvent(new Event('error'))
209+
this.dispatchEvent(new Event('loadend'))
210+
throw error
211+
}
217212
}
218213
}
219214

0 commit comments

Comments
 (0)