javascript - rxjs5 toPromise 不工作

标签 javascript rxjs5

在以下示例中,toPromise 不起作用:

https://jsfiddle.net/tossp/nmf9jg32/

我的代码:

function getPostData() {
    return fetch('https://jsonplaceholder.typicode.com/posts/1')
    .then(res => res.json())
}
var source = Rx.Observable.fromEvent(document.body, 'click');

var example = source.concatMap(
            e => Rx.Observable.from(getPostData()), 
            (e, res, eIndex, resIndex) => res.title);

example.subscribe({
    next: (value) => { console.log('subscribe!!!',value); },
    error: (err) => { console.log('Error: ' + err); },
    complete: () => { console.log('complete'); }
});
example.do((value)=>console.log('do!!!',value)).toPromise().then((value)=>console.log('toPromise!!!',value));

最佳答案

已经解决https://github.com/ReactiveX/rxjs/issues/2536

toPromise 本质上是 observable.last().subscribe()

如果您在调用 toPromise 之前添加 .take(1),那么事情就会开始起作用。

example.do((value)=>console.log('do!!!',value)).take(1).toPromise()

关于javascript - rxjs5 toPromise 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313799/

相关文章:

angular - 错误 rxjs_Observable__.Observable.forkJoin 不是函数?

redux - rxjs 触发并行请求,并等待其中一个请求完成并更新

javascript - 连接 : how to handle each subscribe with a specific next function as independent

javascript - 获取html页面中的所有段落id

javascript - 为什么 `foo()` 在这里工作?

javascript - 如何将谷歌地图设置为移动/视网膜模式(?) - 难以辨认的地方标签

angular - 在 Angular 5 http 请求中使用 observable 有什么好处?

javascript - 动态属性的外部

javascript - jqplot 选项的问题

angular - RxJS 顺序操作