我正在尝试在执行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
});
}
答案 0 :(得分:2)
您是否需要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
});
}