Skip to content

Commit d917f2e

Browse files
committed
Remove .then for async/await
1 parent 85dbff4 commit d917f2e

File tree

3 files changed

+76
-97
lines changed

3 files changed

+76
-97
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"parserOptions": {
3+
"ecmaVersion": 2017
4+
},
25
"extends": [
36
"plugin:github/es6",
47
"plugin:github/browser"

include-fragment-element.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
}, 0)
1212
}
1313

14-
function handleData(el, data) {
15-
return data.then(
16-
function(html) {
17-
const parentNode = el.parentNode
18-
if (parentNode) {
19-
el.insertAdjacentHTML('afterend', html)
20-
parentNode.removeChild(el)
21-
}
22-
},
23-
function() {
24-
el.classList.add('is-error')
25-
}
26-
)
14+
async function handleData(el, data) {
15+
let html
16+
try {
17+
html = await data
18+
} catch (error) {
19+
el.classList.add('is-error')
20+
}
21+
22+
const parentNode = el.parentNode
23+
if (parentNode) {
24+
el.insertAdjacentHTML('afterend', html)
25+
parentNode.removeChild(el)
26+
}
2727
}
2828

2929
const IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype)
@@ -112,42 +112,30 @@
112112
})
113113
}
114114

115-
IncludeFragmentPrototype.load = function() {
116-
const self = this
117-
118-
return Promise.resolve()
119-
.then(function() {
120-
const request = self.request()
121-
fire('loadstart', self)
122-
return self.fetch(request)
123-
})
124-
.then(function(response) {
125-
if (response.status !== 200) {
126-
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
127-
}
128-
129-
const ct = response.headers.get('Content-Type')
130-
if (!ct || !ct.match(/^text\/html/)) {
131-
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
132-
}
133-
134-
return response
135-
})
136-
.then(function(response) {
137-
return response.text()
138-
})
139-
.then(
140-
function(data) {
141-
fire('load', self)
142-
fire('loadend', self)
143-
return data
144-
},
145-
function(error) {
146-
fire('error', self)
147-
fire('loadend', self)
148-
throw error
149-
}
150-
)
115+
IncludeFragmentPrototype.load = async function() {
116+
const request = await this.request()
117+
fire('loadstart', this)
118+
const response = await this.fetch(request)
119+
if (response.status !== 200) {
120+
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
121+
}
122+
123+
const ct = response.headers.get('Content-Type')
124+
if (!ct || !ct.match(/^text\/html/)) {
125+
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
126+
}
127+
128+
let data
129+
try {
130+
data = await response.text()
131+
} catch (error) {
132+
fire('error', this)
133+
fire('loadend', this)
134+
throw error
135+
}
136+
fire('load', this)
137+
fire('loadend', this)
138+
return data
151139
}
152140

153141
IncludeFragmentPrototype.fetch = function(request) {

test/test.js

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -83,72 +83,60 @@ suite('include-fragment-element', function() {
8383
})
8484
})
8585

86-
test('data with src property', function() {
86+
test('data with src property', async function() {
8787
const el = document.createElement('include-fragment')
8888
el.src = '/hello'
8989

90-
el.data.then(
91-
function(html) {
92-
assert.equal('<div id="replaced">hello</div>', html)
93-
},
94-
function() {
95-
assert.ok(false)
96-
}
97-
)
90+
let html
91+
try {
92+
html = await el.data
93+
} catch (error) {
94+
assert.ok(false)
95+
}
96+
assert.equal('<div id="replaced">hello</div>', html)
9897
})
9998

100-
test('data with src attribute', function() {
99+
test('data with src attribute', async function() {
101100
const el = document.createElement('include-fragment')
102101
el.setAttribute('src', '/hello')
103102

104-
el.data.then(
105-
function(html) {
106-
assert.equal('<div id="replaced">hello</div>', html)
107-
},
108-
function() {
109-
assert.ok(false)
110-
}
111-
)
103+
let html
104+
try {
105+
html = await el.data
106+
} catch (error) {
107+
assert.ok(false)
108+
}
109+
assert.equal('<div id="replaced">hello</div>', html)
112110
})
113111

114-
test('setting data with src property multiple times', function() {
112+
test('setting data with src property multiple times', async function() {
115113
const el = document.createElement('include-fragment')
116114
el.src = '/count'
117115

118-
el.data
119-
.then(function(text) {
120-
assert.equal('1', text)
121-
el.src = '/count'
122-
})
123-
.then(function() {
124-
return el.data
125-
})
126-
.then(function(text) {
127-
assert.equal('1', text)
128-
})
129-
['catch'](function() {
130-
assert.ok(false)
131-
})
116+
let text = await el.data
117+
assert.equal('1', text)
118+
el.src = '/count'
119+
try {
120+
text = await el.data
121+
} catch (error) {
122+
assert.ok(false)
123+
}
124+
assert.equal('1', text)
132125
})
133126

134-
test('setting data with src attribute multiple times', function() {
127+
test('setting data with src attribute multiple times', async function() {
135128
const el = document.createElement('include-fragment')
136129
el.setAttribute('src', '/count')
137130

138-
el.data
139-
.then(function(text) {
140-
assert.equal('1', text)
141-
el.setAttribute('src', '/count')
142-
})
143-
.then(function() {
144-
return el.data
145-
})
146-
.then(function(text) {
147-
assert.equal('1', text)
148-
})
149-
['catch'](function() {
150-
assert.ok(false)
151-
})
131+
let text = await el.data
132+
assert.equal('1', text)
133+
el.src = '/count'
134+
try {
135+
text = await el.data
136+
} catch (error) {
137+
assert.ok(false)
138+
}
139+
assert.equal('1', text)
152140
})
153141

154142
test('data is not writable', function() {

0 commit comments

Comments
 (0)