我正在尝试在执行 for 循环后使用数组列表的值。
我的问题是,当我运行我的应用程序时,它会执行下一个函数而无需等待我的 for 循环
这是我的TS代码,
convertToPDF(){
let loader = this.loadingCtrl.create({
content: "Generating..."
})
loader.present().then(_=>{
this.TempNotesImagesList.forEach(n=>{
this.convertToDataURLviaCanvas(n.url, "image/jpeg").then(base64Img => {
this.images.push(base64Img);
});
})
}).then(_=>{
loader.dismiss();
console.log(this.images); //Check images
this.createPdfX(); //convert to PDF :D
});
}
您是否需要convertToDataURLviaCanvas
串联或并联运行?
系列:
convertToPDF() {
let loader = this.loadingCtrl.create({
content: "Generating..."
});
loader.present()
.then( _ => this.TempNotesImagesList.reduce((p, n) => p.then(_ => this.convertToDataURLviaCanvas(n.url, "image/jpeg").then(base64Img => {
this.images.push(base64Img);
})), Promise.resolve())).then(_ => {
loader.dismiss();
console.log(this.images); //Check images
this.createPdfX(); //convert to PDF :D
});
}
平行:
convertToPDF() {
let loader = this.loadingCtrl.create({
content: "Generating..."
});
loader.present()
.then( _ => Promise.all(this.TempNotesImagesList.map(n => this.convertToDataURLviaCanvas(n.url, "image/jpeg").then(base64Img => this.images.push(base64Img)))))
.then(_ => {
loader.dismiss();
console.log(this.images); //Check images
this.createPdfX(); //convert to PDF :D
});
}