loadImg(src){
return new Promise((resolve,reject) =>{
const img = document.createElement('img')
img.onload = () =>{
resolve(img)
}
img.onerror = () =>{
reject(new Error('图片加载失败'))
}
img.src = src
})
}
const url = 'https://img3.sycdn.imooc.com/54584f540001558402200220-140-140.jpg'
loadImg(url).then(img =>{
console.log(img.width);
return img
}).then(img =>{
console.log(img.height);
}).catch(ex =>{
console.error(ex)
})
手写一个Promise加载图片
最新推荐文章于 2024-11-08 16:09:18 发布