当异步在这里时执行一个函数

时间:2018-07-24 13:05:24

标签: javascript angular asynchronous

我使用angular cli 6和angularfire2。我有这两个功能。我想在函数CaptureScreen()中执行SaveDoc()。定义此网址后,我如何执行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); 
}

1 个答案:

答案 0 :(得分:1)

如果我了解您的代码和意图,那么:

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依赖性。