[RxJS] Filtering operator: first

first(predFn, defVal)

 

first can do the work for both "filter" + "take(1)" which filtering the data and end observable.

// RxJS v6+
import { from } from rxjs;
import { first } from rxjs/operators;

const source = from([1, 2, 3, 4, 5]);
//emit first item to pass test
const example = source.pipe(first(num => num === 5));
//output: "First to pass test: 5"
const subscribe = example.subscribe(val =>
  console.log(`First to pass test: ${val}`)
);

 

// RxJS v6+
import { from } from rxjs;
import { first } from rxjs/operators;

const source = from([1, 2, 3, 4, 5]);
//no value will pass, emit default
const example = source.pipe(first(val => val > 5, Nothing));
//output: ‘Nothing‘
const subscribe = example.subscribe(val => console.log(val));

 

[RxJS] Filtering operator: first

上一篇:PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception


下一篇:web--安全知识点