
Security News
PodRocket Podcast: Inside the Recent npm Supply Chain Attacks
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
@dschz/try-catch
Advanced tools
A tiny utility to wrap promises or async functions and return a
[error, data]
tuple — no moretry/catch
boilerplate.
Result<T, E>
npm install @dschz/try-catch
pnpm install @dschz/try-catch
yarn install @dschz/try-catch
bun install @dschz/try-catch
import { tryCatch } from "@dschz/try-catch";
const [err, res] = await tryCatch(fetch("/api/data"));
const [err, user] = await tryCatch(() => fetchUserById(123));
const [err, parsed] = await tryCatch(() => JSON.parse('{"valid":true}'));
if (err) {
console.error("Invalid JSON:", err.message);
}
⚠️ Always wrap expressions that might throw in a function. This ensures the error is caught inside the try-catch scope.
// ✅ CORRECT
await tryCatch(() => JSON.parse("{ malformed }"));
// ❌ INCORRECT — throws before tryCatch is even called
await tryCatch(JSON.parse("{ malformed }"));
type Success<T> = [error: null, data: T];
type Failure<E extends Error = Error> = [error: E, data: null];
type Result<T, E extends Error = Error> = Success<T> | Failure<E>;
The return value is a tuple:
[error, data]; // One will always be null
class MyError extends Error {
constructor(message: string) {
super(message);
this.name = "MyError";
}
}
const [err, data] = await tryCatch<MyType, MyError>(() => doSomething());
MIT © Daniel Sanchez
FAQs
Simple try-catch utility function for JavaScript
The npm package @dschz/try-catch receives a total of 33 weekly downloads. As such, @dschz/try-catch popularity was classified as not popular.
We found that @dschz/try-catch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.