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
function slow(id, cb) { | |
setTimeout(function () { | |
cb(null, id) | |
}, Math.random() * 3000) | |
} | |
let promises = [] | |
for (let i = 0; i < 3; i++) { | |
const prom = new Promise((resolve, reject) => { | |
slow(i, function (err, res) { |
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
export default function App() { | |
const [isOpen, setIsOpen] = React.useState(false); | |
const ref = React.useRef(null); | |
React.useEffect(() => { | |
if (isOpen === true) { | |
const handleEvent = (e) => { | |
const element = ref.current; | |
if (element && !element.contains(e.target)) { |
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 { | |
createContext, | |
memo, | |
useCallback, | |
useContext, | |
useEffect, | |
useId, | |
useLayoutEffect, | |
useMemo, | |
useReducer, |
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
/* | |
Josh Comeau Reset https://www.joshwcomeau.com/css/custom-css-reset/ with my tweaks | |
*/ | |
*, | |
*::before, | |
*::after { | |
box-sizing: border-box; | |
} | |
* { | |
margin: 0; |
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 obj = { | |
[Symbol.toPrimitive]: function (hint) { | |
if (hint === "number") { | |
return 42; | |
} else if (hint === "string") { | |
return "Hello"; | |
} else { | |
return true; | |
} | |
}, |
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 serializableSymbol = Symbol.for("serializable"); | |
class User { | |
constructor(name) { | |
this.name = name; | |
this[serializableSymbol] = ["name", "age"]; // Mark 'name' and 'age' for serialization | |
} | |
getSerializableProperties() { | |
const properties = []; |
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 obj = { | |
[Symbol("my_key")]: 1, | |
[Symbol("second_key")]: 2, | |
enum: 3, | |
nonEnum: 4, | |
}; | |
Object.defineProperty(obj, "nonEnum", { | |
enumerable: false, | |
}); |
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
function binarySearch(list, item) { | |
let min = 0; | |
let max = list.length - 1; | |
let guess; | |
while (min <= max) { | |
guess = Math.floor((min + max) / 2); | |
if (list[guess] === item) { | |
return guess; | |
} else { |
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
function joinElements(arr, joinString) { | |
function recurse(i, resultSoFar) { | |
resultSoFar += arr[i]; | |
console.log(resultSoFar); | |
if (i === arr.length - 1) { | |
return resultSoFar; | |
} else { | |
return recurse(i + 1, resultSoFar + joinString); | |
} | |
} |
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 memoizedClosureTimes10 = () => { | |
let cache = {}; | |
return (n) => { | |
if (n in cache) { | |
console.log(`Fetching from cache: ${n}`); | |
return cache[n]; | |
} else { | |
console.log(`Calculating result~~`); | |
let result = n * 10; |
NewerOlder