在触发下一个事件之前,如何让一个可观察对象等待
How can I let an observable to wait before firing the next event
我在 Angular 项目中使用 RxJs。有两个科目
s1.next()
s1.subscribe(() => {
// does some operation and then triggers s2 event
s2.next()
});
s2.subscribe(() => {
// does some operation and then ends.
});
如何让 s1 等到 s2 完成它的工作。
在您的代码中,您需要显示那是什么类型的工作。
我认为你不需要s2,你需要建立一个基于s1 emits的管道。
如果是请求 - 只需制作一个管道
s1.pipe(
// s1 emits
switchMap(() => {
return this.http.get('url1');
}),
// one more request, once the first one has been finished.
switchMap(() => {
return this.http.get('url2');
}),
).subscribe(response => {
// 2 requests have been completed and we are here.
});
s1.next(); // triggers the flow.
我在 Angular 项目中使用 RxJs。有两个科目
s1.next()
s1.subscribe(() => {
// does some operation and then triggers s2 event
s2.next()
});
s2.subscribe(() => {
// does some operation and then ends.
});
如何让 s1 等到 s2 完成它的工作。
在您的代码中,您需要显示那是什么类型的工作。 我认为你不需要s2,你需要建立一个基于s1 emits的管道。
如果是请求 - 只需制作一个管道
s1.pipe(
// s1 emits
switchMap(() => {
return this.http.get('url1');
}),
// one more request, once the first one has been finished.
switchMap(() => {
return this.http.get('url2');
}),
).subscribe(response => {
// 2 requests have been completed and we are here.
});
s1.next(); // triggers the flow.