如何在角度7中将预订结果从一种方法返回到另一种方法?

时间:2019-03-28 03:58:09

标签: angular async-await es6-promise

我有一种上传文件的方法

 uploadFiles(event, type) {
            var files = event.srcElement.files;
            for (let i = 0; i < files.length; i++) {
                formData.append("uploads[]", files[i], files[i]['name']);
            }

            formData.append('formFlags', type);
            return await this.uploadService.upload(formData).subscribe(response => {
               //return response
            });

上面的上传函数是从另一个函数

调用的
uploadMarketingAgreement(event) {
    this.uploadFiles(event, "profilepic");
   //how to get result of the above method in a varaible
}

如何将第一个(异步)函数的结果返回给第二个函数?

1 个答案:

答案 0 :(得分:0)

我有一个模拟服务,可以返回一个简单的函数... working demo在这里...只要该服务返回调用,就可以使用该值

constructor(private uService: UploadService) {
    this.uploadMarketingAgreement('');
  }

  uploadFiles(event, type) {
    return new Promise((resolve, reject) => {

      this.uService.upload(this.formData).subscribe(
        response => { console.log("data from service:" + response); resolve(response); }
        , errr => { console.log("error from service:" + errr); reject(errr); }
      );
    });
  }

  async uploadMarketingAgreement(event) {
    const uploadFilesVal = await this.uploadFiles(event, "profilepic");
    console.log('from await:' + uploadFilesVal);
    //how to get result of the above method in a varaible
  }