js Promise 返回数组但无法使用
该如何返回结果呢?
let element = document.querySelectorAll('.test')
let promise = new Promise((resolve,reject) => {
let imgs = []
for (var i = 0; i < element.length; i++) {
html2canvas(element[i],{
useCORS: true,
allowTaint: false,
logging: true
}).then(function(canvas) {
let ctx = canvas.getContext("2d")
let page = document.createElement("canvas")
page.width = canvas.width
page.height = canvas.height
page.getContext("2d").putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0)
imgs.push(page.toDataURL("image/jpeg", 1.0))
})
}
console.log(imgs,imgs.length)
resolve(imgs)
})
promise.then(e => {
console.log(e, e.length)
})
在这里打印e,可以看到是个数组,但e.length显示就是0;而且直接使用e[0]就是undefined
将
resolve(imgs)
修改为并移动到
imgs.push(page.toDataURL("image/jpeg", 1.0))
后面