My hardest question so far, damm!
IntuitionI won't lie the moment I saw this question, I had a firm believe that somehow I should be able to solve this problem. It was this belief that kept me h
# Intuition
I won't lie the moment I saw this question, I had a firm believe that somehow I should be able to solve this problem. It was this belief that kept me hanging on this problem for over 3 days (found the solution on the 4th day, pheww...).

# Approach
Again I won't lie the descrition really made it difficult to follow a certain approach for me. I tried out varous ways to get it done. Later, I sticked with the current approach. I pretty much got confused about returning the cancelFn. I also had to look for what others did (had to look for solutions yeah, I coundn't figure on my own on the 4th day so I got really frustrated.) 

Even though I copy pasted one or two solutions, they didn't seem to work at all. I don't know these people also copy pasted someone's solutions or not. 

Nonetheless, I got a hit out of these solutions, that's for sure.And this is the line that made my code to work :

```javascript []
if (t < cancelTimeMs) {
  result.push({ time: startTime - cancelTimeMs, returned: fn(...args) });
}
```


# Complexity
- Time complexity:
O(1)

- Space complexity:
O(1)

# Code
```javascript []
/**
 * @param {Function} fn
 * @param {Array} args
 * @param {number} t
 * @return {Function}
 */

var cancellable = function (fn, args, t) {
  const timer = setTimeout(() => {
    fn(...args);
  }, t);

  return function cancelFn() {
    clearTimeout(timer);
  };
};

const result = [];
const cancelTimeMs = 50;
const fn = (x) => x * 5;
const t = 20;
const args = [2];
const startTime = Math.floor(performance.now());

if (t < cancelTimeMs) {
  result.push({ time: startTime - cancelTimeMs, returned: fn(...args) });
}

const returnedCancelFn = cancellable(fn, args, t);
setTimeout(() => {
  returnedCancelFn();
  console.log(result);
}, cancelTimeMs);






/**
 *  const result = [];
 *
 *  const fn = (x) => x * 5;
 *  const args = [2], t = 20, cancelTimeMs = 50;
 *
 *  const start = performance.now();
 *
 *  const log = (...argsArr) => {
 *      const diff = Math.floor(performance.now() - start);
 *      result.push({"time": diff, "returned": fn(...argsArr)});
 *  }
 *       
 *  const cancel = cancellable(log, args, t);
 *
 *  const maxT = Math.max(t, cancelTimeMs);
 *           
 *  setTimeout(cancel, cancelTimeMs);
 *
 *  setTimeout(() => {
 *      console.log(result); // [{"time":20,"returned":10}]
 *  }, maxT + 15)
 */
```
['JavaScript']