【问题标题】:Equivalent to Promise.try with Observables?等价于带有 Observables 的 Promise.try?
【发布时间】:2018-07-09 14:14:52
【问题描述】:

我如何启动一个带有可能引发异常的函数的 Observable 链,并通过 observable catch 机制处理这个异常?

例子:

createObservable(withFunctionThatThrowAnException())
            .catch( err => {})
            .subscribe(data => {})

如果withFunctionThatThrowAnException 抛出异常,observable 链需要启动并且它只是一个未捕获的异常,observable.catch 无法捕获它。

有了 bluebird 和 Promises,我会做到的:

Promise.try(() => createObservable(withFunctionThatThrowAnException()))
            .then(data => {})
            .catch(err => {})

Observables 有没有等价物?

【问题讨论】:

    标签: promise rxjs observable bluebird


    【解决方案1】:

    我猜直接相当于defer

    Observable.defer(() => createObservable(withFunctionThatThrowAnException()))
     .subscribe(console.log);
    

    【讨论】:

    • 谢谢,它完美且单线;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2023-04-11
    • 2014-04-14
    相关资源
    最近更新 更多