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")})