【问题标题】:Rxjs condition error flowRxjs条件错误流程
【发布时间】:2017-05-19 03:34:01
【问题描述】:

我实际上是在创建一个事务。简化如下:

1) 进行承诺电话。 2) 如果 error 和 error.code === "ConditionalCheckFailedException",忽略错误并继续流而不做任何更改。 3)如果错误,停止流。

下面给我 1 和 3。如果我有某个例外,我想继续直播。那可能吗? ...

目前,我有:

//... stream that works to this point
.concatMap((item) => {
    const insertions = Rx.Observable.fromPromise(AwsCall(item))
        .catch(e => {
            if (e.code === "ConditionalCheckFailedException") {
                return item
            } else {
                throw e;
            }
        });
    return insertions.map(() => item);
})
.concat // ... much the same

【问题讨论】:

  • 谢谢 - 正在从真实代码翻译并遗漏了一个变量。
  • insertions 是一个承诺,对吧?但不是常规承诺,因为常规承诺没有 map 方法...... insertions.map 做什么?它与insertions 的承诺有何关系?
  • 是的,AWS 调用在返回时附加了一个 .promise()。 “插入”是一个 Rx.Observable.fromPromise。
  • 看着this documentation,您似乎错误地使用了observable .catch ...即示例使用return Rx.Observable.throw(e);return Rx.Observable.just(42);
  • 尝试使用承诺catch 而不是可观察的承诺,即将调用链接到AwsCall(…) 而不是fromPromise(…)。从承诺的角度来看,您的代码看起来是正确的

标签: javascript error-handling promise rxjs


【解决方案1】:

所以catch 想要一个提供新 Observable 的函数。

相反,使用这个:

//... stream that works to this point
.concatMap((item) => {
  const insertions = Rx.Observable.fromPromise(AwsCall(item))
    .catch(e => e.code === "ConditionalCheckFailedException"
      ? Rx.Observable.of(item)
      : Rx.Observable.throw(e)
    )
    /* depending on what AwsCall returns this might not be necessary: */
    .map(_ => item)
  return insertions;
})
.concat // ... much the same

来源:http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-catch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多