RxJS tool operator (Angular environment)

a delay operator

The source Observable delays the specified time before starting to emit values.

import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { delay } from 'rxjs/operators/delay';

@Component({
  selector: 'app-util' ,
  templateUrl: './util.component.html' ,
  styleUrls: ['./util.component.css']
})
export class UtilComponent implements OnInit {

  constructor() { }

  by OnInit () {
    console.log(new Date());
    of('Mike', 'Leo').pipe(delay(1000))
      .subscribe(val => {
        console.log(new Date(), val);
      });
  }
}

Two do/tap operators

do and tap are two identical operators for registering Observable's lifecycle events.

import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { tap } from 'rxjs/operators/tap';

@Component({
  selector: 'app-util' ,
  templateUrl: './util.component.html' ,
  styleUrls: ['./util.component.css']
})
export class UtilComponent implements OnInit {

  constructor() { }

  by OnInit () {

    // tap executes 
    of( 'Mikey' , 'Leo' ).pipe( before subscribe
      tap(
        val => {
          console.log('tap next', val);
        },
        null,
        () => {
          console.log('tap complete');
        }
      )
    )
      .subscribe(
        val => {
          console.log('subscribe next', val);
        },
        null,
        () => {
          console.log('subscribe complete');
        }
      );

    // No subscription, tap does not execute 
    of( 'Raph' , 'Don' ).pipe(
      tap(
        val => {
          console.log('tap next', val);
        },
        null,
        () => {
          console.log('tap complete');
        }
      )
    );
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126239&siteId=291194637