[英]wait function to finish in Angular
我与 angular 7 合作。
我想在执行 rest 语句之前等待 function 完成。
在我的示例中,我想在执行 rest 语句之前等待saveAttach function:
this.router.navigate(['/pages/sending']);
当我测试我的代码时,页面在saveAttach function 运行之前被转发到发送页面。
意思是这个声明this.router.navigate(['/pages/sending']);
首先运行
然后this.dropzone.saveAttach(data.body[0].id);
在跑。
这是我的代码:
public sendTransfer() {
try {
this.correspService.sendCorresp(this.correspondence).subscribe(data => {
this.dropzone.saveAttach(data.body[0].id);
this.router.navigate(['/pages/sending']);
});
} catch (exception) {
console.log('sendCorresp() function error : ' + exception);
}
}
在 dropzone 组件中,我有这个 function:
public saveAttach(id: string) {
let latt: Attachement[] = new Array<Attachement>();
this.dropzoneservice.getDoc({
page: 0,
uuid: localStorage.getItem('uuid'),
sort: ['id']
}).subscribe((res: HttpResponse<IDocTemp[]>) => {
for (let i = 0; i < res.body.length; i++) {
let att = new Attachement();
att.idDoc = res.body[i].id.toString();
att.filename = res.body[i].filenamedoc;
att.id = id;
latt.push(att)
}
this.dropzoneservice.addAtt(latt).subscribe(data => {
console.log("save saveAttach : ", data.body);
})
});
}
使用await
或thenable
方法:
this.dropzone.saveAttach(data.body[0].id)
.then(() => {
this.router.navigate(['/pages/sending']);
});
})
public saveAttach => async(id: string) {
return new Promise(resolve => {
let latt: Attachement[] = new Array<Attachement>();
this.dropzoneservice.getDoc({
page: 0,
uuid: localStorage.getItem('uuid'),
sort: ['id']
}).subscribe((res: HttpResponse<IDocTemp[]>) => {
for (let i = 0; i < res.body.length; i++) {
let att = new Attachement();
att.idDoc = res.body[i].id.toString();
att.filename = res.body[i].filenamedoc;
att.id = id;
latt.push(att)
}
this.dropzoneservice.addAtt(latt).subscribe(data => {
console.log("save saveAttach : ", data.body);
resolve(); // <------------------
})
});
})
}
你应该在你的saveAttach
function 中返回一个 Observable。
import { Observable } from 'rxjs';
import { mergeMap, tap } from 'rxjs/operators';
public saveAttach(id: string): Observable<any> {
let latt: Attachement[] = new Array<Attachement>();
return this.dropzoneservice.getDoc({
page: 0,
uuid: localStorage.getItem('uuid'),
sort: ['id']
}).pipe(
mergeMap(() => {
for (let i = 0; i < res.body.length; i++) {
let att = new Attachement();
att.idDoc = res.body[i].id.toString();
att.filename = res.body[i].filenamedoc;
att.id = id;
latt.push(att);
}
return this.dropzoneservice.addAtt(latt);
}),
tap((data) => {
console.log("save saveAttach : ", data.body);
})
);
}
然后,在sendTransfer
中,您可以 pipe 并一个接一个地执行异步 function 。
import { tap, mergeMap, catchError } from 'rxjs/operators';
public sendTransfer() {
this.correspService.sendCorresp(this.correspondence).pipe(
catchError((exception) => console.log('sendCorresp() function error : ' + exception)),
mergeMap((data) => this.dropzone.saveAttach(data.body[0].id)),
tap(() => this.router.navigate(['/pages/sending']))
).subscribe();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.