Skip to content

Instantly share code, notes, and snippets.

View psawn's full-sized avatar

Đoàn Đức Bảo psawn

View GitHub Profile
import { ResultAsync } from 'neverthrow';
type Success<T> = { data: T; error: null };
type Failure<E> = { data: null; error: E };
type Result<T, E = Error> = Success<T> | Failure<E>;
export async function tryCatch<T, E = Error>(
promise: Promise<T>,
import { useState, Activity } from "react";
export default function App() {
const [show, setShow] = useState(true);
return (
<>
<button onClick={() => setShow(prev => !prev)}>
Toggle
</button>
// Case 1
type ButtonConfig = {
variant: 'primary' | 'secondary';
size: 'small' | 'default';
};
const button1: ButtonConfig = {
variant: 'primary',
size: 'small',
};
type User = {
name: 'psawn';
};
interface LoadingState {
status: 'loading';
}
interface SuccessState {
status: 'success';
const req = {
method: 'GET',
url: 'api',
} as const;
// ^ "as const" converts fields from 'string' to literal types (e.g., exactly 'GET').
// It also makes the object read-only, preventing changes.
function makeReq(url: string, method: 'GET' | 'POST') {
return '';
}
@psawn
psawn / setTimeout.js
Created December 5, 2025 16:33
This snippet is taken from the internet and show a comparison between a custom Promise-based sleep function and Node.js's built-in async sleep
// -----------------------------
// 1. Custom sleep using Promise
// -----------------------------
async function testCustomSleep() {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
console.log("Sleeping (custom Promise)...");
await sleep(1000);
console.log("Done (custom Promise)!");
}