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
。我认为代码是自我解释的,所以如果有人对此有更好的方法,那就太感谢了