Rxjs toPromise 行为与 observable 不同 [英] Rxjs toPromise behavior different than observable

查看:28
本文介绍了Rxjs toPromise 行为与 observable 不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的例子,其中两个方法创建并返回一个承诺.第二个方法buildRedCar() 调用第一个方法buildCar(),修改promise 返回的值并从自身返回另一个promise.代码然后调用 buildRedCar(),并且只是 console.logs 结果.这在转换为 Promise 时不起作用,但在使用直接 observables 时确实起作用.

I have a simple example where two methods create and return a promise. The second method buildRedCar() calls the first method buildCar(), modifies the value returned by the promise and returns another promise from itself. The code then calls buildRedCar(), and just console.logs the result out. This does not work when converting to promises, but it does work when using straight observables.

不起作用:

import * as Rx from 'rx';

function buildCar(): Rx.IPromise<string> {
    let car = 'Car';
    return Rx.Observable.just<string>(car).toPromise();
}

function buildRedCar(): Rx.IPromise<string> {
    let observable = Rx.Observable.create<string>((observer) => {
        buildCar().then((car) => {
            observer.onNext('Red ' + car);
        });
    })

    return observable.toPromise();
}

buildRedCar().then((car) => {
    console.log(car);
});

是否有效:

import * as Rx from 'rx';

function buildCar(): Rx.Observable<string> {
    let car = 'Car';
    return Rx.Observable.just<string>(car);
}

function buildRedCar(): Rx.Observable<string> {
    let observable = Rx.Observable.create<string>((observer) => {
        buildCar().subscribe((car) => {
            observer.onNext('Red ' + car);
        });
    })

    return observable;
}

buildRedCar().subscribe((car) => {
    console.log(car);
});

当唯一的区别是在返回之前将 observable 转换为 promise 时,为什么会有不同的行为?

Any idea why the different behavior when the only difference is converting the observable to a promise before returning?

推荐答案

当 RxJs 将 Observable 转换为 Promise 时,它​​会创建一个 Promise,该 Promise 将从 observable 中产生 last 值.因此,在底层 observable 完成之前,promise 不会解析.

When RxJs converts an Observable to a Promise, it creates a Promise that will yield the last value from the observable. Thus the promise will not resolve until the underlying observable completes.

在您的第一个示例中(以及在您的第二个示例中),您从未完成可观察的红色汽车.因此,承诺永远不会解决.因此包装 Promise 的 observable 永远不会产生值......

In your first example (and in your second as well), you never complete the red car observable. Therefore the promise never resolves. Therefore the observable that wraps the promise never produces a value...

您的第二个示例有效,因为订阅急切地打印红色汽车 observable 到达时产生的第一个值,然后等待"永远不会到达的进一步结果.

Your 2nd example works because the subscription eagerly prints the first value produced by your red car observable as it arrives, and then "waits" for further results that never arrive.

添加对 onComplete 的调用应该会使您的第一个版本正常工作:

Adding a call to onComplete should make your first version work:

observer.onNext('Red ' + car);
observer.onComplete();

这篇关于Rxjs toPromise 行为与 observable 不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆