-
Book Overview & Buying
-
Table Of Contents
Modern JavaScript Applications
By :
In programming, lazy evaluation is a strategy that delays the evaluation of values until they're needed. There are two means by which lazy evaluation is implemented by Bacon.js.
A stream or property will not be attached to its data source until it has subscribers. Let's look at an example to understand this. Place this code in the index.js file:
var myButton_click_stream1 = $("#myButton").asEventStream("click").map(function(event){
console.log(event);
return event;
});Here, when you click on the myButton button, nothing will be logged. Now, place this code in the index.js file:
myButton_click_stream1.onValue(function(event){})Now when you click on the button, the event will be logged.
The log method is also considered as a subscriber.
Methods such as map and combine* use lazy evaluation to avoid evaluating events and values that aren't actually needed. Lazy evaluation results in huge performance benefits in some cases.
But how do map and combine* know whether an...