我有一种上传文件的方法
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
}
如何将第一个(异步)函数的结果返回给第二个函数?
答案 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
}