function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
}
想知道的是這個方法怎麼用呢?我輸入了URL
,然後再then
裡獲取物件image?可是我測了,沒有反應!
滿天的星座2017-05-16 13:31:18
loadImageAsync('./img/news-1.png').then((img) => {
document.getElementById("app").appendChild(img)
console.log(img)
})
這麼調用,在then的參數就是resolve的參數, 也就是image對象,把這個對象append到p上就可以實現功能
我想大声告诉你2017-05-16 13:31:18
可以的 ,我在瀏覽器裡試過了:
function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
}
loadImageAsync('https://www.baidu.com/img/bd_logo1.png').then(function(){alert("jiazai wancheng")})