function imgLoad (url) {
return new Promise ( (resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = 'blob'
console.log('使用 XHR 加载图像', xhr.status)
xhr.onload = () => {
if(xhr.status === 200) {
resolve(xhr.response)
} else {
reject (Error('Image do not load successfully, error code:' + xhr.statusText) )
}
}
xhr.onerror = () => {
reject(Error('There was a network error'))
}
xhr.send()
})
}
let body = document.querySelector('body')
let myImage = new Image()
// imgLoad('XXX.jpg').then( (response) => {
imgLoad('https://mdn.mozillademos.org/files/8633/promises.png').then( (response) => {
let imageUrl = window.URL.createObjectURL(response)
myImage.src = imageUrl
body.addendChild(imageUrl)
}, (Error) => {
console.log('then--error', Error)
})
console