[英]wait function to finish in Angular
I work with angular 7.我与 angular 7 合作。
I want to wait function until finish before execute the rest of statement.我想在执行 rest 语句之前等待 function 完成。
In my example I want to wait saveAttach function before execute the rest of statement:在我的示例中,我想在执行 rest 语句之前等待saveAttach function:
this.router.navigate(['/pages/sending']);
When I test my code the page is forwarded to sending page before the saveAttach function is running.当我测试我的代码时,页面在saveAttach function 运行之前被转发到发送页面。
meaning this statement this.router.navigate(['/pages/sending']);
意思是这个声明
this.router.navigate(['/pages/sending']);
is running first首先运行
and then this.dropzone.saveAttach(data.body[0].id);
然后
this.dropzone.saveAttach(data.body[0].id);
is running.在跑。
this is my code:这是我的代码:
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);
}
}
and in dropzone component I have this function:在 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);
})
});
}
Use await
or thenable
method:使用
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(); // <------------------
})
});
})
}
You should return an Observable in your saveAttach
function.你应该在你的
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);
})
);
}
Then, in sendTransfer
you can pipe and execute you asynchronous function one after the other.然后,在
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.