Pure Memoization in JavaScript | Closures Example

function square(num) {
  return num * num;
}

function memoizedSquare() {
  let cache = {}
  return function optimizedSquare(num) {
    //console.log(JSON.stringify(cache));
    if(num in cache) {
      console.log('returning from cache);
      return cache[num];
    }else {
      console.log('computing square');
      const result = square(num);
      cache[num] = result;
      return result;
    }
  }
}

const memoSquare = memoizedSquare();
console.log(memoSquare(10));
console.log(memoSquare(20));
console.log(memoSquare(30));
console.log(memoSquare(30));

What is Closures is javascript

Closures 

Function along with its lexical scope bundled together forms a closure. it's called Closures.

How to cancel/abort ajax request in axios

useEffect(() => {    const ourRequest = Axios.CancelToken.source() // <-- 1st step      const fetchPost = async () => {      try {        const response = await Axios.get(`endpointURL`, {          cancelToken: ourRequest.token, // <-- 2nd step        })        console.log(response.data)        setPost(response.data)        setIsLoading(false)      } catch (err) {        console.log('There was a problem or request was cancelled.')      }    }    fetchPost()      return () => {      ourRequest.cancel() // <-- 3rd step    }  }, [])