简体   繁体   English

这个三元运算符表达式的等价物是什么?

[英]What is the equivalent of this ternary operator expression?

I have to use a piece of code that comes from another company project.我必须使用来自另一个公司项目的一段代码。 Unfortunately, it contains an expression that triggers an error in SonarCloud.不幸的是,它包含一个在 SonarCloud 中触发错误的表达式。 The error is:错误是:

Non-empty statements should change control flow or have at least one side-effect非空语句应该改变控制流或至少有一个副作用

The colleague that wrote this line is not in the company anymore.写这行的同事已经不在公司了。

The line that needs to be modified is xhr.status === 200? observable.next(xhr.response), observable.complete()): observable.error(xhr.statusText);需要修改的行是xhr.status === 200? observable.next(xhr.response), observable.complete()): observable.error(xhr.statusText); xhr.status === 200? observable.next(xhr.response), observable.complete()): observable.error(xhr.statusText); . .

Here is the full code:这是完整的代码:

  sendMedia(file: File, presignedUrl: string): Observable<Object> {
    return new Observable(observable => {
      const xhr = new XMLHttpRequest();
      xhr.open('PUT', presignedUrl, true);
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4) {
          xhr.status === 200 ?
            (observable.next(xhr.response), observable.complete()) :
            observable.error(xhr.statusText);
        }
      };
      xhr.send(file);
    });
  }

If this block equivalent to that statement?如果这个块相当于那个语句?

if (xhr.status === 200) {
  return observable.next(xhr.response), observable.complete();
} else {
  return observable.error(xhr.statusText);
}

Thanks a lot for anyone trying to help!非常感谢任何试图提供帮助的人!

You are almost there except return statement你几乎就在那里,除了返回声明

if (xhr.status === 200) {
  observable.next(xhr.response);
  observable.complete();
} else {
  observable.error(xhr.statusText);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM