等待 foreach 的执行以在 ionic 2 中继续下一步

wait the execution of foreach to continue next in ionic 2

我正在尝试在执行 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
    });
}