等待foreach的执行继续下一个离子2 [英] wait the execution of foreach to continue next in ionic 2

查看:135
本文介绍了等待foreach的执行继续下一个离子2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在执行for循环后使用数组列表的值。

I'm trying to use the values of an array list after executing the for loop.

我的问题是,当我运行我的应用程序时,它执行下一个函数没有等待我的循环

My problem is that when I run my application, it executes the next functions without waiting my for loop

这是我的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 以串行或并行方式运行?

Did you need the convertToDataURLviaCanvas to run in series or parallel?

系列:

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
    });
}

这篇关于等待foreach的执行继续下一个离子2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆