Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 为什么在subscribe中的第一次回调之前运行完全回调之后_Angular_Typescript_Callback_Asynccallback - Fatal编程技术网

Angular 为什么在subscribe中的第一次回调之前运行完全回调之后

Angular 为什么在subscribe中的第一次回调之前运行完全回调之后,angular,typescript,callback,asynccallback,Angular,Typescript,Callback,Asynccallback,我有两个方法名saveAsPdf和downloadPdf。这两种方法都包含async await,这可能导致在saveAsPdf之前运行downloadPdf方法,这是不需要的。 我试着使用承诺,但结果是一样的 pdfBytes; saveAsPDf() { this.storage.get("pageCanvasJson").subscribe((pagesCanvas: {}) => { for (let page in pagesCanv

我有两个方法名
saveAsPdf
downloadPdf
。这两种方法都包含async await,这可能导致在
saveAsPdf
之前运行
downloadPdf
方法,这是不需要的。 我试着使用承诺,但结果是一样的

pdfBytes;

saveAsPDf() {
    this.storage.get("pageCanvasJson").subscribe((pagesCanvas: {}) => {
        for (let page in pagesCanvas) {
            let url;
            const pages = this.pdfDoc.getPages();
            if (pagesCanvas[page].objects.length > 0) {
                this.canvas.loadFromJSON(pagesCanvas[page],async () => {
                    url = this.rasterize();
                    console.log(url);
                    const pngBytes = await fetch(url).then((res) => res.arrayBuffer());
                    const pngImage = await this.pdfDoc.embedPng(pngBytes);
                    const currentPage = pages[+page - 1];
                    const { width, height } = currentPage.getSize();
                    const pngDim = pngImage.scale(width / this.size.width);
                    currentPage.drawImage(pngImage, {
                        x: 0,
                        y: 0,
                        width: pngDim.width,
                        height: pngDim.height,
                    });
                    this.pdfBytes = await this.pdfDoc.save();
                    this.pdfDoc = await PDFDocument.load(this.pdfBytes);
                    });
            }
        }
    },
    (err) => console.error(err),
    () => this.downloadPdf());
        
}
async downloadPdf() {
    console.log("??????????????")
    this.pdfBytes = await this.pdfDoc.save();
    var blob = new Blob([this.pdfBytes], {
        type: "application/pdf",
    });
    FileSaver.saveAs(blob, this.pdfName);
    this.clearPdfSrc.emit("clear");
    this.pdfSrc = "";
    this.canvas.clear();
}
我只想在运行
saveAsPdf()
方法之后运行
downloadPdf()
方法,以便获得最终的
pdfBytes
。我认为代码是自我解释的,所以如果有人对此有更好的方法,那就太感谢了