大家好, 链接 RXJS5 结果的最佳方式是什么,比如 promise ?
interface MyObj{
name : string
url: string
html: any // async
}
// promise chaining, pretty simple
getMyObjWithPromise()
.then(myObj=>{
// promise, we get back html from myObj.url async
return getMyObjHtmlWithPromise(myObj)
})
.then(myObj=>{
// done, here we have myObj with html
})
与RXJS5类似? 我们需要跨流共享 myObj,并异步修改 obj 属性...
最佳答案
通过 Promises 和链接 then()
调用,您可以修改传递给连续处理程序的结果。
RxJS 中最相似的选项是 map()
运算符或concatMap()
如果你想返回另一个 Observable。在某些情况下也do
可能有用,但它不能修改传递的值。
Rx.Observable.fromPromise(getMyObjWithPromise())
.map(myObj => {
return myObj;
})
.concatMap(myObj => {
// promise, we get back html from myObj.url async
return Rx.Observable.fromPromise(getMyObjHtmlWithPromise(myObj));
})
.subscribe(myObj => {
// done, here we have myObj with html
});
请注意,通常您需要至少有一个订阅者才能使 Observable 发出值。
关于javascript - RXJS5 像 Promise 一样链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40881903/