This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEfftect, useRef } from "react"; | |
const useStateWithcallback = (initialState) => { | |
const [state, setState] = useState(initialState); | |
const callbackRef = useRef(null); | |
const setStateWithCallback = (newState, callback) => { | |
callbackRef.current = callback; | |
setState(newState); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem Statement | |
//Implement a function in JavaScript that caches the API response for the given amount of time. | |
//If a new call is made between that time, the response from the cache will be returned, | |
//else a fresh API call will be made. | |
/** | |
* | |
* @param {*} expiryTime in ms | |
*/ | |
function cachedApi(expiryTime) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem Statement | |
//Implement a function in JavaScript that caches the API response for the given amount of time. | |
//If a new call is made between that time, the response from the cache will be returned, | |
//else a fresh API call will be made. | |
/** | |
* | |
* @param {*} expiryTime in ms | |
*/ | |
function cachedApi(expiryTime) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Array map polyfill | |
* @param {*} callback | |
* @returns | |
*/ | |
function myMap(callback) { | |
let newArr = []; | |
for (let i = 0; i < this.length; i++) { | |
newArr.push(callback(this[i])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cache = []; | |
function fib(num) { | |
if (cache[num] == undefined) { | |
if (num == 0) { | |
cache[0] = 0; | |
} else if (num == 1) { | |
cache[1] = 1; | |
} else { | |
cache[num] = fib(num - 1) + fib(num - 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**generates async tasks */ | |
function asyncTask() { | |
return new Promise((resolve, reject) => { | |
let val = Math.floor(Math.random() * 10); | |
if (val > 5) { | |
resolve(val); | |
} else { | |
reject(val); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EEStoEventMaps = new WeakMap(); | |
export default class EventEmitter{ | |
constructor(publisher){ | |
const eventMap = Object.create(null); |