如何解决为什么在完成回调之后在订阅中的第一个回调之前运行
我有两个方法名saveAsPdf
和downloadPdf
。这两个方法都包含async-await,可能导致downloadPdf
方法在saveAsPdf
之前运行,这是不必要的。
我尝试使用Promise,但结果是相同的。
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();
}
我只想在运行downloadPdf()
方法后才运行saveAsPdf()
方法,以便获得最终的pdfBytes
。我认为代码是不言自明的,因此,如果有人对此有更好的方法,那将是感激的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。