1

i came across this documentation on angular pipe.

  • i tried out custom pipes without ajax calls(transformations like touppercase,tolowercase) and i receive expected results.
  • i tried out custom pipes with ajax call as mentioned in the documentation, and here is my observation.

logic in pipe class(impure pipe) (code copied from the documentation linked to this post) enter image description here

logic in template (code copied from the documentation linked to this post) enter image description here

output enter image description here

The output makes me to think about few things

  • is it recommended to use custom pipes for http calls based on the results i have posted above?
  • if i had not used the condition in line15,22 to 26 then the http calls are triggered infinite times. This example mentioned in this documentation doesn't make any sense to me for practical use(production use). why not remove this example and discourage from using this approach? or am i missing to understand some concepts here? please let me know
  • When i use the condition in line15,22 to 26 then the http call is invoked only once, but the customPipe gets invoked few additional number of times. why is that? No objects/variables in components (except for logic in pipe component) were updated to trigger the change detection
divine
  • 4,746
  • 3
  • 27
  • 38
  • First red flag, you're trying to return the async variable `this.cachedData` synchronously. By the time you're returning, it hasn't been assigned the value from the HTTP call yet. So it'd returning the previous value. You'd need to return the observable and `async` it in the template. Also please don't post code snippets as screenshots. It makes it difficult to reproduce the issue. – ruth Oct 08 '20 at 11:14
  • @MichaelD yes, but this is the example given in the documentation. you can refer the code from the documentation linked to the question if you would want to check the code – divine Oct 08 '20 at 12:11
  • please don't post screenshots of your code – Christian Vincenzo Traina Oct 08 '20 at 12:24
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Christian Vincenzo Traina Oct 08 '20 at 12:25
  • @CristianTraìna i have mentioned in my question that the screenshot of the code snippet is taken from the documentation link the i have posted . the answer link you have shared is completely unrelated to my question. i request you to read the question properly – divine Oct 08 '20 at 12:34
  • @divine: In that case the documentation is wrong. If the pipe is impure then yes having an HTTP call inside it would be invoked way too many times (once for each change detection cycle). Though the link might not seem relevant to the question, you still need to understand **_why_** the HTTP call is bad inside an impure pipe. – ruth Oct 08 '20 at 12:43
  • @MichaelD `you still need to understand why the HTTP call is bad inside an impure pipe` - impure pipe looks for any change in value than change in reference? but all the calls to the http request will create new observable, so even if i use pure pipe it the outcome will be same as impure pipe? is my understanding wrong? is it even possible to properly trigger a http call inside custompipe? – divine Oct 08 '20 at 13:01

0 Answers0