我想实现像这样的链接方法调用
observable
.pipe(
filter('foo'),add(3)
)
.subscribe(subscriber);
要使其正常工作,.pipe(...)
的结果必须提供方法subscribe
。
我想允许通过pipe
进行某些链接方法调用(例如async
)。但是,这会中断我的工作,因为pipe
返回的promise没有subscribe
方法:
await observable
.pipe(
filter('foo'),add(3)
)
.subscribe(subscriber);
async pipe(...operators){
...
}
=> Uncaught (in promise) TypeError: observable.pipe(...).subscribe is not a function
我可以将我的主要代码重写为
observable
.pipe(
filter('foo'),add(3)
).then(pipeResult=>
pipeResult.subscribe(subscriber);
);
但是,我觉得阅读起来很丑。
=>有没有一种方法可以对方法调用链中的每个调用都应用await
,而不仅仅是最后一个?
我希望有类似的东西
awaitEach observable
.pipe(
filter('foo'),add(3)
)
.subscribe(subscriber);
编辑
- 相关问题:
chaining async method calls - javascript
- 借助Promises,我可以从同步调用转换为异步调用:
foo(){
return new Promise(resolve=>{
baa().then(arg=>resolve(arg))
})
}
但是,我需要另一个方向,例如:
pipe() {
var result = undefined;
await asyncCall(()=>{ //await is not allowed here; forces pipe to by async
result = 5;
});
return result;
}