Skip to content

Commit 9f9e6ea

Browse files
committed
Revert "Remove .then for async/await"
This reverts commit d917f2e.
1 parent 01c7051 commit 9f9e6ea

File tree

3 files changed

+97
-76
lines changed

3 files changed

+97
-76
lines changed

.eslintrc.json

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
"parserOptions": {
3-
"ecmaVersion": 2017
4-
},
52
"extends": [
63
"plugin:github/es6",
74
"plugin:github/browser"

include-fragment-element.js

+49-37
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
}, 0)
1212
}
1313

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-
}
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+
)
2727
}
2828

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

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
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+
)
139151
}
140152

141153
IncludeFragmentPrototype.fetch = function(request) {

test/test.js

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

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

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)
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+
)
9798
})
9899

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

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)
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+
)
110112
})
111113

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

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)
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+
})
125132
})
126133

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

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)
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+
})
140152
})
141153

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

0 commit comments

Comments
 (0)