Create a new function that calls the provided fn with the given arguments. After the first call the return value will be cached and returned again, meaning you technically only need to pass the arguments the first time.
fn
Time complexity: O(1)
Space complexity: O(1)
The function to thunkify
A function that returns whatever the first call of fn returns for the given arguments
Higher order
function add(a, b) { return a + b;}const thunk = thunkify(add);thunk(1, 2); // 3thunk(3, 4); // 3 Copy
function add(a, b) { return a + b;}const thunk = thunkify(add);thunk(1, 2); // 3thunk(3, 4); // 3
Create a new function that calls the provided
fn
with the given arguments. After the first call the return value will be cached and returned again, meaning you technically only need to pass the arguments the first time.Time complexity: O(1)
Space complexity: O(1)