Skip to content

Commit 9cc95a0

Browse files
committed
Move onerror/onload logic into main function.
This allows better reject handling, e.g. rejecting exceptions which are not Error objects.
1 parent efadb68 commit 9cc95a0

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

js/load-image.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,20 @@
107107
img.src = url
108108
}
109109
img.onerror = function (event) {
110-
return loadImage.onerror(img, event, file, url, reject, options)
110+
revokeHelper(url, options)
111+
if (reject) reject.call(img, event)
111112
}
112-
img.onload = function (event) {
113-
return loadImage.onload(img, event, file, url, resolveWrapper, options)
113+
img.onload = function () {
114+
revokeHelper(url, options)
115+
var data = {
116+
originalWidth: img.naturalWidth || img.width,
117+
originalHeight: img.naturalHeight || img.height
118+
}
119+
try {
120+
loadImage.transform(img, options, resolveWrapper, file, data)
121+
} catch (error) {
122+
if (reject) reject(error)
123+
}
114124
}
115125
if (typeof file === 'string') {
116126
if (loadImage.requiresMetaData(options)) {
@@ -169,26 +179,6 @@
169179
callback(img, data)
170180
}
171181

172-
loadImage.onerror = function (img, event, file, url, callback, options) {
173-
revokeHelper(url, options)
174-
if (callback) {
175-
callback.call(img, event)
176-
}
177-
}
178-
179-
loadImage.onload = function (img, event, file, url, callback, options) {
180-
revokeHelper(url, options)
181-
182-
try {
183-
loadImage.transform(img, options, callback, file, {
184-
originalWidth: img.naturalWidth || img.width,
185-
originalHeight: img.naturalHeight || img.height
186-
})
187-
} catch (error) {
188-
callback(error)
189-
}
190-
}
191-
192182
// Loads a given File object via FileReader interface,
193183
// invokes the callback with the event object (load or error).
194184
// The result can be read via event.target.result:

0 commit comments

Comments
 (0)