AddThis

Sunday, 8 July 2018

Promises vs Observables in Angular

Promises
  1. Definition : Helps you run functions asynchronously, and use their return values (or exceptions) but only once when executed.
  2. Not Lazy
  3. Not cancellable. The two possible decisions are
    • Reject
    • Resolve
  4. Cannot be retried(Promises should have access to the original function that returned the promise in order to have a retry capability, which is a bad practice)
Observables
  1. Definition : Helps you run functions asynchronously, and use their return values in a continous sequence(multiple times) when executed.
    1. By default, it is Lazy as it emits values when time progresses. It means until it is subscribed, it doesn't execute the function.
  2. Has a lot of operator which simplifies coding effort.
  3. One operator retry can be used to retry when ever needed, also if we need to retry the observable based on some conditions retryWhen can be used.
    Note: A list of operators along with their interactive diagrams is available here at RxMarbles.com

No comments:

Post a Comment

Solving real time queries using java 8 features stream api with examples

package com.pse; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java...