S
S
Skrolea2016-03-10 19:40:11
JavaScript
Skrolea, 2016-03-10 19:40:11

What is the difference between map, observable, subscribe, promise?

Good afternoon. Do not explain what to what of the above in the title?
I need to do

getVar() {
    
      return this.http.get('test.json')
          .map(res => res.json())
          .toPromise()
          .then(pageTitleBase => pageTitleBase.filter(article => article.id === id)[0]);    
  }

What happens when I add to return this.http.get('test.json')either map or toPromise() or subscribe? What depends on what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-03-10
@Skrolea

map - for each result
toPromise - convert an Observable to a promise
subscribe - subscribe to changes in an observable
difference between observable and promises - promises only work once, observable can be restarted many times. That is, instead of remembering the config, we literally create an object that describes the request (the real request is not sent before subscribe / map is called). If your network has fallen off and we want to repeat the request, it is enough to call the object method again, and not create a new request. Tritely more convenient, especially in terms of code reuse (you can make a request interceptor at the service level that would handle such situations for us).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question