Mos*_*ito 5 observable rxjs typescript angular
我有3个可观察链,我需要链接,第一个用于另一个2.它们必须按顺序运行,每个必须等待前一个完成.是否有可能在没有嵌套的情况下进行链接?
public observable1(): Observable<Response> {
let headers = new Headers();
headers.append("Content-Range", "bytes */*");
let requestOptions = new RequestOptions({headers: headers});
return this.http.put(url, "", requestOptions);
}
public observable2(data1url): Observable<Response> {
let headers = new Headers();
headers.append("Content-Range", "bytes */*");
let requestOptions = new RequestOptions({headers: headers});
return this.http.put(data1url, "", requestOptions);
}
public observable3(data1url): Observable<Response> {
let headers = new Headers();
headers.append("Content-Range", "bytes */*");
let requestOptions = new RequestOptions({headers: headers});
return this.http.put(data1url, "", requestOptions);
}
Run Code Online (Sandbox Code Playgroud)
这是我发现链接它们的唯一方法,但它们仍然是嵌套的,是否可以这样做而不是嵌套它们?
public doHttpProcess() {
return this.observable1().concatMap(result1 => {
return this.observable2(result1.json().data1)
.concatMap(result2 => {
return this.observable3(result1.json().data1);
});
});
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助
你就快到了,你只需要删除第二个嵌套,你就可以链接concatMap调用
public doHttpProcess() {
return this.observable1()
.concatMap(result1 => this.observable2(result1.json().data1).map(result2 => ({ result1, result2 }))
.concatMap(({ result1, result2 }) => this.observable3(result1.json().data1));
}
Run Code Online (Sandbox Code Playgroud)
如果你不需要result2生成最后一个可观察的,你可以简化为
public doHttpProcess() {
return this.observable1()
.concatMap(result1 => this.observable2(result1.json().data1).mapTo(result1))
.concatMap(result1 => this.observable3(result1.json().data1));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |