What is the use of pipe() and map() functions in Angular 6?

Asked

Viewed 2,689 times

1

I learned that I have to use these two functions when I’m working with HTTP requests, I learned that I have to use these two functions as a cake recipe, which you use and that’s it, I wanted to know why to use it.

Example of a use of the two functions:

    public pesquisaOferta(termo: string): Observable<Oferta[]> {
    return this.http.get(URL_API + "?descricao_oferta_like=" + termo)
    .pipe(
        retry(10),
        map((response: any) => {
            return response
        })
    )
}

If I’m not mistaken, I’ve seen the use of the two functions in another way, but I’m in line with why to use them.

1 answer

7


The function pipe() serves to read data from a source as soon as they become available and write this data in another location.

For example, in your HTTP request example you make the request and the data will come slowly, as each piece of this data arrives, it becomes available within the function pipe()

The function map() creates a new array with the data of the array it maps, iterating over each element. It is widely used to perform actions according to each element.

In that reply from Stackoverflow, you can read more about some specific advantages on why to use these two functions.

  • He simply specified the two functions. Thank you

  • In the context of the example I gave above, the pipe() takes the information from get() and makes available to the map() ?

  • @Exact Erickluz.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.