typescript 等待 observable 完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45354940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:45:52  来源:igfitidea点击:

Wait for observable to complete

javascriptangulartypescriptrxjsobservable

提问by developer

I have series of methods which are dependent on completion of other methods.

我有一系列方法依赖于其他方法的完成。

process1(data: string) : Observable<string> {
   this.dataservice.process(data).subscribe(
            (response) => {
                return response.data;
            }
        );
}

main(data: string) : string {

   var process1Data: string = process1(data); 

   // I would like to wait for process1 method to complete before running process2
   // I do not want to include process2 inside subscribe of process1 because I have to make few more method calls
   var process2Data: string = process2(process1Data);

   var process3Data: string = process3(process2Data);

   ...

}

How can I wait for an observable to complete before calling next method (process2, process3)? (similar like await in c#)

如何在调用下一个方法(process2,process3)之前等待可观察对象完成?(类似于c#中的await)

回答by dee zg

You might try something like this...

你可以试试这样的...

main(data: string) : string {

    process1Data$: Observable<string> = process1(data)
        .take(1)
        .switchMap((process1Data) => return process2(process1Data);
    .
    .
    .
}

Obviously, take(1)assumes that process1(...)resolves to single value and stops. After that it switchMaps to process2which means it starts emitting whatever observable from process2gives. If, on the other hand, you want process2to be ran of each result emitted from process1then just remove take(1).

显然,take(1)假设process1(...)解析为单个值并停止。之后它switchMapsprocess2意味着它开始发出任何可观察到的东西process2。另一方面,如果您想process2运行从中发出的每个结果,process1则只需 remove take(1)

回答by Aleksandr Petrovskij

You can use es6 async/await

你可以使用 es6 async/await

async main(data: string): string {
    var process1Data: string = await process1(data).toPromise();
    var process2Data: string = process2(process1Data);
    ...
}

回答by Steve

You can use rxjs concat operator. See documentation here. concat

您可以使用 rxjs concat 运算符。请参阅此处的文档。 连接

Basically it waits untill the first or source observable returns and then executes next.

基本上它一直等到第一个或源可观察对象返回,然后执行下一个。

update

更新

You can also try operators like switch or switchmap according to your requirements.

您还可以根据需要尝试 switch 或 switchmap 等运算符。

回答by Ambrose Leung

process1in the original question is confusing as it does not return an Observable<string>(Maybe I'm using another Observablefrom 'rxjs/Observable').

process1在原始问题中令人困惑,因为它没有返回Observable<string>(也许我正在使用Observable来自“rxjs/Observable”的另一个)。

This is the code I'm referring to (in original question):

这是我指的代码(在原始问题中):

process1(data: string) : Observable<string> {
   this.dataservice.process(data).subscribe(
            (response) => {
                return response.data;
            }
        );
}

For me, I changed it to this:

对我来说,我把它改成了这样:

process1(data: string) : Observable<string> {
   return this.dataservice.process(data).map(  //subscribe-->map
            (response) => {
                return response.data;
            }
        );
}

Then to have something happen after process1completes, just use subscribelike you would with any other Observable:

然后在process1完成后发生一些事情,就像使用subscribe其他任何东西一样Observable

main(data: string) : string {
   process1(data).subscribe((process1RetVal)=>
   {
         process2(process1RetVal);
   });
}