用angular中的flatMap完成

时间:2019-02-07 05:50:38

标签: angular rxjs

我正在使用angular 5和rxjs。我正在拨打2个服务电话,其中一个取决于其他结果。我正在使用flatMap。在完成两个api调用之后,我还想执行一些操作。我正在为此使用finalize。我的代码如下所示:

this.myservice.api1(param1).pipe(takeUntil(this.destroyed$), finalize(() => {
//do something after both api calls are completed
},
flatMap((result1) => {
    //do some operation and create object x(this.objx)
    return this.myservice.api2(param1);
})
).subscribe((result2) => {
    //do something based on result2 and this.objx
})

但是,finalize块仅在第一次调用API之后执行。使用一个API调用可以执行finalize,但是使用flatMap则只能在第一次API调用之后执行。请让我知道我是否有误。

1 个答案:

答案 0 :(得分:0)

我想export const options = async (event, context, callback) => { const ALLOWED_ORIGINS = [ 'http://localhost:3001', ] const origin = event.headers.origin || event.headers.Origin let response if (ALLOWED_ORIGINS.includes(origin)) { response = { statusCode: 200, headers: { 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Origin': origin, 'Access-Control-Allow-Credentials': true, }, body: JSON.stringify({ message: 'Success' }), } } else { response = { statusCode: 403, headers: { 'Access-Control-Allow-Origin': '*', }, body: JSON.stringify({ message: { error: 'Missing Authentication token', origin: `Request origin is ${origin}`, }, }), } } callback(null, response) return } 是正常的http请求,它只发出一次然后完成。

我建议为此使用this.myservice.api1(param1)

switchMap

最终完成将在链完成或出错时调用。