Note
unwrap-go brings Go-style error handling to TypeScript and JavaScript, offering a clean and predictable way to handle errors using Result tuples. This approach eliminates the need for try-catch blocks and provides a more functional approach to error management.
unwrap-go features intro:
- Go-Style Error Handling β Uses tuple returns (
[data, error]
) similar to Go's approach, making error handling more explicit and predictable - Promise Support β Built-in support for handling asynchronous operations with the same elegant syntax
- Type Safety β Full TypeScript support with generic types for both success and error cases
- Zero Dependencies β Lightweight implementation with no external dependencies
- Synchronous Support β Handle both async and sync operations with consistent patterns
You can add unwrap-go with one command via:
npm install @arnavk-09/unwrap-go
deno add jsr:@arnavk-09/unwrap-go
bun add @arnavk-09/unwrap-go
pnpm add @arnavk-09/unwrap-go
yarn add @arnavk-09/unwrap-go
OR
deno add jsr:@arnavk-09/unwrap-go
npm install @arnavk-09/unwrap-go
bun add @arnavk-09/unwrap-go
pnpm add @arnavk-09/unwrap-go
yarn add @arnavk-09/unwrap-go
import { unwrapPromise, unwrapSync } from "@arnavk-09/unwrap-go";
// Async example
async function fetchData() {
const [data, err] = await unwrapPromise(
fetch("https://api.example.com/data"),
);
if (err) {
console.error("Failed to fetch:", err);
return;
}
console.log("Data:", data);
}
// Sync example
function divide(a: number, b: number) {
const [result, err] = unwrapSync(() => {
if (b === 0) throw new Error("Division by zero");
return a / b;
});
if (err) {
console.error("Division failed:", err);
return;
}
console.log("Result:", result);
}
Function | Description | Parameters | Return Type |
---|---|---|---|
unwrapPromise<T, E> |
Wraps a promise to return a Result tuple | promise: Promise<T> |
Promise<Result<T, E>> |
unwrapSync<T, E> |
Executes a synchronous function and returns a Result tuple | fn: () => T |
Result<T, E> |
Result<T, E> |
Type representing either success (T) or error (E) | T : Success typeE : Error type |
[T, null] | [null, E] |
Task | Status |
---|---|
π§ͺ Add tests | π΄ |
π§ͺ Publish To JSR | π’ |
π§ͺ Publish To NPM | π’ |
Tip
We welcome contributions to improve unwrap-go! If you have suggestions, bug fixes, or new feature ideas, follow these steps:
-
Fork the Repository you Click the Fork button at the top-right of the repo page.
-
Clone Your Fork
Clone the repo locally: YOUgit clone https://github.com/ArnavK-09/unwrap-go.git
-
Create a Branch
Create a new branch for your changes:git checkout -b your-feature-branch
-
Make Changes
Implement your changes (bug fixes, features, etc.). -
Commit and Push
Commit your changes and push the branch:git commit -m "feat(scope): description" git push origin your-feature-branch
-
Open a Pull Request
Open a PR with a detailed description of your changes. -
Collaborate and Merge
The maintainers will review your PR, request changes if needed, and merge it once approved.
Found a bug or need help? Please create an issue on the GitHub repository with a detailed description.
![]() Arnav K |
unwrap-go is licensed under the Unlicense
License. See the LICENSE file for more details.
π If you find this project helpful, please give it a star on GitHub! π