Skip to content

Instantly share code, notes, and snippets.

@thibmeu
thibmeu / depromise.ts
Created August 30, 2023 12:17
DON'T DO THIS. Block the thread to resolve a promise synchronously
const depromise = <T>(promise: Promise<T>): T => {
let result: T | undefined;
let error: Error | undefined;
let isPromisePending = true;
promise
.then((res) => {
result = res;
isPromisePending = false;
})
@thibmeu
thibmeu / uint8array.js
Last active March 15, 2024 09:39
Uint8Array helper for encoding and decoding, similar to Node.js `Buffer`
// Helper methods
const hex_decode = (s) =>
Uint8Array.from(s.match(/.{1,2}/g).map((b) => parseInt(b, 16)));
const hex_encode = (u) =>
Array.from(u).map((b) => b.toString(16).padStart(2, '0')).join('')
const ascii_decode = (s) =>
Uint8Array.from(Array.from(s).map((l) => l.charCodeAt(0)))
@thibmeu
thibmeu / 1on1questionsgenerator.js
Last active January 20, 2021 20:12
Get a random question for your next 1 on 1. Demo at https://getone.one
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const getList = () =>
fetch(
'https://raw.githubusercontent.com/VGraupera/1on1-questions/master/questions.json'
)
.then(r => r.json())
@thibmeu
thibmeu / loader.js
Last active October 27, 2019 22:07
Loader in URL
// Issue is it polutes user's history
const loader = (prefix='', frames = ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'], index = 0, interval = 100) =>
setInterval(() => {
history.pushState(null, '', `${prefix}${frames[index]}`)
index = (index+1) % frames.length
}, interval)
// To add `loader` as a searchParams
let url = new URL(location.href)
url.searchParams.append('loader', '')