The pipe method of the Observable object in rxjs

Source code:

import { of } from 'rxjs';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { tap } from 'rxjs/operators';

@Injectable()
export class JerrySandBoxService{
    name = 'Jerry';
    print(){
        const observable = of(1, 2, 3);
        const newObservable = observable.pipe(
            tap(num => console.log(num)),
            map(num => 'hello world: ' + num)
        );
        newObservable.subscribe(data => console.log(data));
    }
}

Execute the map function before executing the pipe:

map returns a new function:

Then execute the pipe function: operations variable length parameters, including tap and map two operations.


The implementation of pipeFromArray uses JavaScript's closure closure:

So far, the arrow functions in the tap and map we passed in have not been executed until the subscribe method is called:

At this time, for 1, 2, and 3 in the array, first execute the tap and map operations in the pipeline, and then use the output of the map operation as input to execute the callback specified in the subscribe:

To get more original articles by Jerry, please follow the public account "Wang Zixi":

Guess you like

Origin blog.csdn.net/i042416/article/details/108600630