angular - 在 Observable 中调用 Promise
问题描述
我必须Promise
在另一个 API 调用(returns)之后进行 API 调用(returns Observable
)。
userService.getAuthUser()
返回Observable
一个.
userService.updateUser()
返回Promise
一个。
我可以通过把updateUser()
里面getAuthUser()
this.userService.getAuthUser().subscribe((res) =>{
this.userService.updateUser(res.id, <User>({
name: 'Sample Name'
})).then((res) =>{
console.log('update user success');
}).catch((err) => {
console.log('update user failed');
})
},
(err) => {
console.log('get auth user failed');
})
但我觉得这样做不太好,有点回调地狱,还有更好的方法吗?
注意:我无法更改userService.updateUser()
为Observable
.
解决方案
有几种方法可以实现这一目标。
如果您希望 getAuthUser 流保持活动状态,一种方法是使用from 运算符将 promise 转换为 observable 。这将允许您继续流并对整个流的错误/处理成功做出反应。您还可以使用catchError 运算符指定流中对特定错误做出反应的位置。
类似于:
this.userService.getAuthUser()
.pipe(
catchError(err => {
// throw an error how you like, see the 'throw' operator
// can also create with other operators like 'of'
}),
switchMap(auth =>
from( // will detect promise and return observable
this.userService.updateUser(res.id, <User>({
name: 'Sample Name'
}))
)
),
catchError(err => {
// throw an error how you like, see the 'throw' operator
// can also create with other operators like 'of'
})
).subscribe(
(res) => {
// if you were expecting data at this point
}, (err) => {
// if you threw the error
}
)
另一种方法是,如果您不需要流保持活动状态,您可以将 Observable 从第一个流转换为带有.toPromise()
. 从这里开始,您有两条典型的路径可供遵循。您可以使用async/await,或者只是链接承诺。
对于异步等待,类似于:
// the method will have to be async for await to not show errors
const authUser = await this.userService.getAuthUser().toPromise();
// determine if you should update
// if so
const updateUserResult = await this.userService.updateUser(res.id, <User>({name: 'Sample Name'}));
推荐阅读
- amazon-web-services - S3 存储桶策略允许 S3 访问 AWS 控制台中当前经过身份验证的用户?
- typescript - Pulumi 根据选择的栈修改 kubernetes 资源
- oracle - 我们可以在 OBIEE 中创建带有时间线的动态信息图表吗?
- java - 实现DnD时如何让jtable transferhandler高亮一行?
- azure - 在 Azure Function App 中使用 SignalR 并在门户上发布
- r - 有什么方法可以保护我的 R 源代码吗?
- rest - REST 实体版本
- python - 从一列pandas python中的字符串切片创建新列
- node.js - DynamoDB:无法更新节点 js 中的 dynamoDB 项
- java - 将字符串分成 3 部分并在 JAVA 中导出为 CSV