What is cold observable in context of angular HttpClient

自闭症网瘾萝莉.ら 提交于 2019-12-11 01:47:59

问题


While using angular HttpClient of angular I got to know that HttpClient post method used cold observable and will make 2 separate calls to post data to the server.And also unless you will not subscribe the post method it will not post data to the server.

Although, Rxjs cold observable says it will hold all the sequence until the the end and fire all when it subscribed.

How it will make 2 separate call to server to post data.


回答1:


I don't believe that this behavior is caused by the use of observables.

Rather I suspect that the browser is issuing a pre-flight OPTIONS request as a 'handshake' with the server to determine whether the CORS protocol is understood. This precedes the POST request and is possibly why you get 2 calls to the server to post data.




回答2:


COLD is when your observable creates the producer

// COLD
var cold = new Observable((observer) => {
  var producer = new Producer();
  // have observer listen to producer here
});

HOT is when your observable closes over the producer

// HOT
var producer = new Producer();
var hot = new Observable((observer) => {
  // have observer listen to producer here
});

source: https://medium.com/@benlesh/hot-vs-cold-observables-f8094ed53339



来源:https://stackoverflow.com/questions/46079030/what-is-cold-observable-in-context-of-angular-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!