Reputation: 11376
Can anyone explain what the difference is between the following 2 sources? I came across the first block but not sure why it would be preferable to the second.
source 1
Observable
.of(futureDate)
.flatMap(date => {
const delay = date - Date.now();
return Observable.timer(delay);
});
source 2
const delay = futureDate - Date.now();
Observable.timer(delay);
Upvotes: 1
Views: 170
Reputation: 58430
The difference becomes apparent when you consider that nothing happens until a subscription is made - and subscriptions could be made sometime after the observables have been created.
When a subscription is made to the first observable, the delay passed to the timer is based upon the current time - that is, the time of subscription.
When a subscription is made the the second, the timer's delay will have been based upon the time at which the observable was created - not upon the time of subscription.
Upvotes: 3