我有从数据链接数组上传文件的功能我想做的是如果数据链接数组包含 3 个文件const testLinks = 3;并且async uploadImageData被解雇了三次我想在被解雇了三次console.log之后。uploadImageData我正在考虑进行计数,但我所有的测试都会在每次被触发时重新开始计数uploadImageData。.tsasync uploadImageData(formData: FormData) { const testLinks = 3; const uploadlink = answerAtachmentUrl; const headers = headerLink; const loading = await this.loadingController.create({ message: 'Uploading Photos and Files...', }); await loading.present(); this.httpClient.post<any>( uploadlink + this.userToken, formData, { 'headers':headers } ).pipe( finalize(() => { loading.dismiss();}) ) .subscribe(res => { if (res['success']) { setTimeout(() => { this.DeleteAllFiles(); }, 5000); this.presentToastPhoto('Photo sync success.'); } else { this.presentToastPhoto('Photo upload failed.'); let respFail = JSON.stringify(res); console.log("respFail", respFail); } }); // console.log fires once after count and const testLinks both equal 3}
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
“我正在考虑进行计数,但我所有的测试都从每次 uploadImageData 被触发时开始计数。” 您没有发布您尝试过的内容,但您可能没有将计数器设为全局变量。您的代码显然是一个更大项目的一部分,因此我只是做了这个小测试来表明它可以与异步函数一起使用。如果不起作用,请告诉我。
var count=0;
async function uploadImageData() {
const testLinks = 3;
count++;
console.log(count);
if (count === 3){
console.log('count = 3');
}
}
uploadImageData();
uploadImageData();
uploadImageData();
uploadImageData();
添加回答
举报
0/150
提交
取消