为什么RxJS subscription允许省略arrow函数和以下方法参数? [英] Why does RxJS subscribe allow omitting the arrow function and the following method argument?

查看:88
本文介绍了为什么RxJS subscription允许省略arrow函数和以下方法参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我需要使用RxJS.我试图设计一个错误处理流程,但发现一些奇怪的语法传递方法参数:

Recently I needed to use RxJS. I tried to design an error handling flow, but I discovered some weird syntax passing method arguments:

.subscribe(
    x => {
    },
    console.warn // <- Why does this compile, and warn 'is not 7' in debug console?
);

链接到最小复制:

https://stackblitz.com/edit/rxjs-6-5-error-handle-no-arrow-issue

  1. 使用RxJS 6.5
  2. 创建一个可观察到的函数
  3. 订阅可观察
  4. 将参数传递给订阅
  5. 只使用,console.warn,不像,error => { console.warn(error); }
  1. Use RxJS 6.5
  2. Create a function return observable
  3. Subscribe to observable
  4. Pass parameter into subscribe
  5. Just use ,console.warn, not like ,error => { console.warn(error); }

没有箭头功能,它仍然将错误传递给console.warn.为什么?

Without an arrow function, it still passes errors to console.warn. Why?

代码:

import { throwError, concat, of } from "rxjs";
import { map } from "rxjs/operators";

const result = concat(of(7), of(8));

getData(result).subscribe(
  x => {
    console.log("typeof(x)", typeof(x));
    if (typeof(x) === 'string') {
      console.log("x  Error", x);
      return;
    }
    console.log("no error", x);
  },
  console.warn // <- Why does this compile, and warn 'is not 7' in debug console?
);

// pretend service method
function getData(result) {
  return result.pipe(
    map(data => {
      if (data !== 7) {
        throw "is not 7";
      }
      return data;
    })
  );
}

我尝试用Google搜索一些关键字,js,rxjs,angular,省略箭头功能,缺少参数......但我无法找到此处使用的技术.

I tried to google some keywords, js, rxjs, angular, omit arrow function, argument missing,... but I cannot locate what tech is used here.

任何人都可以提供指向该机制的解释链接吗?

以下两个问题是相关的,但没有解释行为,仅说等效":

The following two questions are related but do not explain the behavior, just say "equivalent":

map(this.extractTreeData)

map(this.extractTreeData)

等同于

map(tree => this.extractTreeData(tree))

map(tree => this.extractTreeData(tree))

如何将额外的参数传递给RxJS地图运算符

为什么链映射操作符中缺少参数

推荐答案

在JS中

In JS functions are first class objects. When you have the code console.warn no brackets you have a reference to this object but you're not invoking that object, that would require braces console.warn(). For example you can do:

let x = console.warn;
console.log('not happened yet');
x('test');

因此,您的代码很容易以与传递任何其他函数(例如

So your code is simple passing the console.warn function to the parameter of the Subscribe failure in exactly the same manner as you might pass any other function, e.g.

Subscribe(() => {}, () => {});

[为什么]显示警告不是7"

另一部分是您抛出错误throw "is not 7";.因此,Subscribe错误调用的签名是:

[why] show Warn 'is not 7'

The other part of this is that your throwing an error throw "is not 7";. The signature of the error call of Subscribe is thus:

subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;

因此,error的参数的类型为any.因此,throw将Error传递给错误函数处理程序.设置为console.warn,其签名为:

So the parameter of error is of type any. So the throw passes an Error to the error function handler. This is set as console.warn which has a signature of :

console.warn(obj1 [, obj2, ..., objN]);

console.warn本质上将传递给它的任何参数转换为字符串,JS不是强类型化的,这本质上可以归结为

console.warn essentially turns whatever parameter it's passed into a string, JS is not strongly typed and this is essentially down to type coercion, and logs it. the string of throw "is not 7"; is is not 7. So it logs is not 7.

总而言之,我想说的都是有点晦涩难懂,而且可能很难遵循.从技术上讲,这里没有什么错,但是我想说,做以下事情更有意义:

All in all I'd say this is all all a bit cryptic and potentially difficult to follow. There is nothing technically wrong here, but I would say it would make more sense to do the following:

.subscribe(
    x => {
    },
    x => {console.warn(x);} 
);

基于>任何傻瓜都可以编写计算机可以理解的代码的原则.好的程序员编写人类可以理解的代码."

这篇关于为什么RxJS subscription允许省略arrow函数和以下方法参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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