Skip to content

Instantly share code, notes, and snippets.

View adamelliotfields's full-sized avatar
🤗
hf.co/adamelliotfields

Adam Fields adamelliotfields

🤗
hf.co/adamelliotfields
View GitHub Profile
@adamelliotfields
adamelliotfields / wrap.js
Last active July 25, 2023 23:39
Express Async Handler Wrapper to Prevent Unhandled Promise Rejections
/**
* @template {(...args: any[]) => Promise<any>} T
* @param {T} fn
* @returns {(...args: Parameters<T>) => Promise<ReturnType<T>|void>}
*/
export const wrap = (fn) => (...args) => fn(...args).catch(args[2]);
@adamelliotfields
adamelliotfields / server.ts
Last active February 3, 2019 19:03
Node HTTP Server Destroy Sockets
/*!
* Destroy all open connections so a server can close.
* Inspired by https://github.com/marten-de-vries/killable
*/
import http from 'http';
import { Socket } from 'net';
// Set of open sockets
const sockets: Set<Socket> = new Set();