-
Notifications
You must be signed in to change notification settings - Fork 1
/
non-pure-functions.js
41 lines (32 loc) · 1000 Bytes
/
non-pure-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Impure Function
let count = 0;
let price = 9;
const addItem = () => {
count++;
}
const getTotal = () => count * price;
addItem(); //Set to 1 product
/**
* Save for later, we think we should get 0
* But, because we took too long AND
* something changed, we get 18
*/
setTimeout(() => {
console.log('Impure Expects 9', getTotal())
}, 1000);
console.log('Impure Expects 9', getTotal()); //0
addItem();
addItem();
console.log('Impure Expects 27', getTotal()); //18
//--- Impure Generator Example
//Impure generator/factory
// This function can't reliably be tested because it
// outputs differently even with the same input
const createItem = (name, price) => ({
id: Math.floor(Math.random() * 1000),
name,
price
})
console.log(createItem('Peanuts', 10)); // { id: 89, name: 'Peanuts', price: 10 }
console.log(createItem('Peanuts', 10)); // { id: 22, name: 'Peanuts', price: 10 }
console.log(createItem('Peanuts', 10)); // { id: 100, name: 'Peanuts', price: 10 }