请问这个promise怎么取不到返回值啊?
哪位高手救小弟一命,帮我看看drawFn()函数里 promise返回值怎么取不到呢?这是按钮点击时执行的代码,去调用 drawFn()。但在 drawFn().then 里,没法打印 “====压缩完后图片地址”。 有点特别的是drawFn()函数执行时调用了自己。
licence_pic_get(event) {
//从本地选多张图片
wx.chooseImage({
count: 9,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: res => {
this.pageData.tempFilePath = res.tempFilePaths;
this.pageData.compress_tempFilePath = [];
this.pageData.index = 0;
//调用函数,这里没能进入then打印结果,是为什么呢?
this.drawFn().then(res_drawFn => {
console.log('====压缩完后图片地址:', res_drawFn)
})
}
})
},
/////////////////
下面是调用的函数:
drawFn: function () {
let _this = this;
return new Promise((resolve, reject) => {
console.log('第' + _this.pageData.index + '次调用函数');
if (_this.pageData.index < _this.pageData.tempFilePath.length) {
ctx.drawImage(_this.pageData.tempFilePath[_this.pageData.index], 0, 0, 100, 100);
ctx.draw(false, () => {
console.log('第' + _this.pageData.index + '次draw');
wx.canvasToTempFilePath({
width: 100,
height: 100,
destWidth: 100,
destHeight: 100,
canvasId: 'compress',
success: res_compress => {
console.log('第' + _this.pageData.index + '次循环tempFilePath:', res_compress.tempFilePath);
_this.pageData.compress_tempFilePath.push(res_compress.tempFilePath);
_this.pageData.index++;
_this.drawFn(); //调用自己
}
})
})
}
else {
console.log('调用完成compress_tempFilePath:', _this.pageData.compress_tempFilePath);
resolve({
compress_tempFilePath: _this.pageData.compress_tempFilePath,
});
}
}) //promise end
},
/////////////////
下面是打印结果:
没执行: this.drawFn().then(res_drawFn => {
console.log('====压缩完后图片地址:', res_drawFn)
})