• do / tap
    • 签名: do(nextOrObserver: function, error: function, complete: function): Observable
  • Transparently perform actions or side-effects, such as logging.
  • 透明地执行操作或副作用,比如打印日志。
    • 示例
      • 示例 1: 使用 do 输出日志
  • 其他资源

    do / tap

    签名: do(nextOrObserver: function, error: function, complete: function): Observable

    Transparently perform actions or side-effects, such as logging.

    透明地执行操作或副作用,比如打印日志。


    :bulb: If you are using as a pipeable operator, do is known as tap!


    do - 图2

    示例

    示例 1: 使用 do 输出日志

    ( jsBin |
    jsFiddle )

    1. import { of } from 'rxjs/observable/of';
    2. import { tap, map } from 'rxjs/operators';
    3. const source = of(1, 2, 3, 4, 5);
    4. // 使用 tap 透明地打印 source 中的值
    5. const example = source.pipe(
    6. tap(val => console.log(`BEFORE MAP: ${val}`)),
    7. map(val => val + 10),
    8. tap(val => console.log(`AFTER MAP: ${val}`))
    9. );
    10. // 'tap' 并不转换值
    11. // 输出: 11...12...13...14...15
    12. const subscribe = example.subscribe(val => console.log(val));

    其他资源

    • do :newspaper: - 官方文档
    • 使用 do 打印流 :video_camera: :dollar: - John Linquist
    • 工具操作符: do :video_camera: :dollar: - André Staltz

    :file_folder: 源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/do.ts