{"action":"created","issue":{"url":"https://api.github.com/repos/angular/angular/issues/5876","labels_url":"https://api.github.com/repos/angular/angular/issues/5876/labels{/name}","comments_url":"https://api.github.com/repos/angular/angular/issues/5876/comments","events_url":"https://api.github.com/repos/angular/angular/issues/5876/events","html_url":"https://github.com/angular/angular/issues/5876","id":122041789,"number":5876,"title":"Do Observables make sense for http?","user":{"login":"thelgevold","id":349548,"avatar_url":"https://avatars.githubusercontent.com/u/349548?v=3","gravatar_id":"","url":"https://api.github.com/users/thelgevold","html_url":"https://github.com/thelgevold","followers_url":"https://api.github.com/users/thelgevold/followers","following_url":"https://api.github.com/users/thelgevold/following{/other_user}","gists_url":"https://api.github.com/users/thelgevold/gists{/gist_id}","starred_url":"https://api.github.com/users/thelgevold/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thelgevold/subscriptions","organizations_url":"https://api.github.com/users/thelgevold/orgs","repos_url":"https://api.github.com/users/thelgevold/repos","events_url":"https://api.github.com/users/thelgevold/events{/privacy}","received_events_url":"https://api.github.com/users/thelgevold/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":36,"created_at":"2015-12-14T13:26:00Z","updated_at":"2015-12-30T02:03:04Z","closed_at":"2015-12-16T03:13:21Z","body":"I don't want this to sound too critical :-). \r\n\r\nI just have a few questions/concerns about Observables in the context of Angular 2.0 http.\r\n\r\nSince I started using http in Angular 2.0 I have always wondered why a decision was made to go with Observables over promises. \r\n\r\nDon't get me wrong, I think observables (with or without RxJs) is cool and I've enjoyed learning about it, but when it comes to plain http calls, I am struggling to see a clear advantage over regular promises.\r\n\r\nOne of the arguments I've seen is that promises are only good for one time calls, but a stream can be left open indefinitely. My view though is that \"one time\" http calls probably make up the 90% use case for http. I can definitely see an application of the open ended streams, but in my experience I see it more as a one off case in most business applications. Basically now, I find myself using Observables in a way that just mimics promises.\r\n\r\nI see other applications of Observables though. Some examples I've seen include streams of events (e.g. click event patterns etc). I think it definitely has merit, but it seems to me that we are trying to \"shoehorn\" the 90% use case into the 10% use case with this.\r\n\r\nI am not against incorporating alternative technologies, but I question if this pivot to Observables is worth the reeducation of developers when it comes to the http calls. I wonder if it would make sense to offer a lean promise based http implementation to the masses and encourage people with real streaming needs to use RxJs directly instead?  I think that will allow us to benefit from the best of both worlds and better enable us to pick the best tool for the job. \r\n\r\nJust to be clear: My questions/concerns about this predates the new approach of having to bring in RxJS dependencies piecemeal :-). \r\n\r\n-Tor"},"comment":{"url":"https://api.github.com/repos/angular/angular/issues/comments/167917360","html_url":"https://github.com/angular/angular/issues/5876#issuecomment-167917360","issue_url":"https://api.github.com/repos/angular/angular/issues/5876","id":167917360,"user":{"login":"thelgevold","id":349548,"avatar_url":"https://avatars.githubusercontent.com/u/349548?v=3","gravatar_id":"","url":"https://api.github.com/users/thelgevold","html_url":"https://github.com/thelgevold","followers_url":"https://api.github.com/users/thelgevold/followers","following_url":"https://api.github.com/users/thelgevold/following{/other_user}","gists_url":"https://api.github.com/users/thelgevold/gists{/gist_id}","starred_url":"https://api.github.com/users/thelgevold/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thelgevold/subscriptions","organizations_url":"https://api.github.com/users/thelgevold/orgs","repos_url":"https://api.github.com/users/thelgevold/repos","events_url":"https://api.github.com/users/thelgevold/events{/privacy}","received_events_url":"https://api.github.com/users/thelgevold/received_events","type":"User","site_admin":false},"created_at":"2015-12-30T02:03:04Z","updated_at":"2015-12-30T02:03:04Z","body":"I have thought of a simpler case where Observables actually greatly simplify things. In the past I've implemented countless master detail grids where a user clicks a row to load (ajax) more details about a particular item. Along with this we need a way to prevent multiple calls in flight from the user clicking rapidly on multiple rows before the server responds. Without a way to cancel the current request we will have to either temporarily detach event handlers or otherwise disable the row for clicking. \r\nWith the unsubscribe feature of Observables this seems trivial since I can just unsubscribe before each call.\r\n\r\nNot sure if this is the most elegant implementation, but it seems to work:\r\nThe idea here is that I hope the unsubscribe will prevent the results from being processed out of order and match the current row with the results belonging to a different row. Assuming the results of prior calls will simply be ignored and only the results of the most recent request will be processed on the client.\r\n\r\nI would appreciate any ideas for improvements on this. To test it out I added a simple country/capitol grid to my Angular-2-Sample project. \r\nDemo here: http://www.syntaxsuccess.com/angular-2-samples/#/demo/http\r\n\r\n```\r\ngetCapitol(country){\r\n\r\n        //Can this be done differently?\r\n        if(this.pendingRequest){\r\n            this.pendingRequest.unsubscribe();\r\n            console.log('cancelled observable');\r\n        }\r\n\r\n        this.activeCountry = country;\r\n\r\n        this.pendingRequest = this.http.get('./country-info/' + country + '.json')\r\n                              .map((res: Response) => res.json())\r\n                              .subscribe(res => this.capitol = res.capitol);\r\n    }\r\n```\r\n\r\nI guess this is a variation on the type to search filter, but I think it might be a more common feature since master detail grids are so common. I guess you could argue that we could add cancellation to promises, but at least with Observables it's supported out of the box"}}