javascript - 当异步在这里时执行一个函数
问题描述
我使用 angular cli 6 和 angularfire2。我有这两个功能。我想在我的函数 CaptureScreen() 中执行 SaveDoc()。定义此 url 时如何执行 SaveDoc?谢谢
public captureScreen() {
var data = document.getElementById('contentToConvert');
html2canvas( data ).then(canvas => {
var imgWidth =297;
var pageHeight = 210;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
const contentDataURL = canvas.toDataURL('image/png')
var doc = new jspdf('l', 'mm', 'a4'); // A4 size page of PDF
var position = 2;
doc.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
doc.save();
const file = doc.output("blob");
const Patientid = this.patientid;
const filePath = Date.now().toString();
const fileRef = this.storage.ref(Patientid +'/ppss/' + filePath+'.pdf');
const task = this.storage.upload(Patientid +'/ppss/' + filePath+'.pdf', file);
this.uploadPercent = task.percentageChanges();
task.snapshotChanges().pipe(
finalize(() => {
this.downloadURL = fileRef.getDownloadURL();
this.downloadURL.subscribe(url => this.url = url
)
}))
.subscribe();
});
}
SaveDoc(){
const Patientid = this.patientid;
const name = Date.now().toString();
const newDoc = new Doc(name, Patientid, this.url);
console.log(this.url);
this.docsService.CreatePpsDoc(newDoc);
}
解决方案
如果我了解您的代码和您的意图,那么:
task.snapshotChanges()
.pipe(finalize(() => {
this.downloadURL = fileRef.getDownloadURL();
this.downloadURL.subscribe(url => {
// <-- Here, your actual URL is available.
this.url = url;.
// --> So you may call your function here.
this.SaveDoc();
})
})
).subscribe();
你只需要“等待”,调用另一个函数,让你的 downloadURL 可用,然后你可以调用你的另一个函数,因为它有一个 downloadURL 依赖项。
推荐阅读
- javascript - Swiper JS - Enter 按钮不会在缩略图上执行任何操作以实现可访问性
- anaconda - 为什么 Anaconda 安装程序在 CentOS 上失败?
- docker - 更新 docker 服务镜像或 dockerfile 基础镜像有哪些好的策略
- android-studio - Flutter Bloc 中 mapEventToState 中的当前状态不是 yield
- python - 创建一个从不重复随机数的列表
- pine-script - Pine:如何根据条件设置背景颜色
- jquery - 用javascript填充时选择框外的值
- python - Python - 从CSV行+列读取返回行号
- ios - 如何在 Xcode、Swift 中拖放枚举?
- airflow - GCP Composer/Airflow 调用 Dataflow/beam throwing 错误