Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript rxjs5 toPromise不工作_Javascript_Rxjs5 - Fatal编程技术网

Javascript rxjs5 toPromise不工作

Javascript rxjs5 toPromise不工作,javascript,rxjs5,Javascript,Rxjs5,在以下示例中,toPromise不起作用: 我的代码: 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(

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

我的代码:

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));
已经解决

toPromise
本质上是可观察的。last().subscribe()

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


在较新版本中,您必须在
pipe()内使用
take(1)
。我编写了这样的代码:

async getPromise() {

    return await example
    .pipe(take(1))
    .toPromise();

  }

希望它能帮助别人。

toPromise所做的承诺从未得到解决-你认为应该如何实现?pipe(take(1))让我的承诺生效ng9.1.1 rxjs6.5.3谢谢!!!为了让它发挥作用,我简直疯了!还应使用first()运算符:)
async getPromise() {

    return await example
    .pipe(take(1))
    .toPromise();

  }