January 2020
Intermediate to advanced
470 pages
11h 13m
English
If a function becomes impure because it needs to call another function that is itself impure, a way around this problem is to inject the required function in the call. This technique actually provides more flexibility in your code and allows for easier future changes, as well as less complex unit testing.
Let's consider the random filename generator function that we saw earlier. The problematic part of this function is its usage of getRandomLetter() to produce the filename:
const getRandomFileName = (fileExtension = "") => { ... for (let i = 0; i < NAME_LENGTH; i++) { namePart[i] = getRandomLetter(); } ...};
A way to solve this is to replace the impure function with an injected external one; we must now provide ...
Read now
Unlock full access