Skip to main content
Home

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
52%
Published
a year ago (1.0.0)

doublet

Handle any Javascript and Typescript function as an error and response tuple. This prevents declaring variables outside of a try/catch block for a cleaner flow.

Forked from mats852/doublet to return an object instead of an array.

Installation

npm i @billyjacoby/doublet

Usage

Traditionnal error handling

import doublet from 'doublet';
import HttpException from 'your-favorite-error-handler';

async function fetchUser(id: string): User {
  let user;

  try {
    user = await fetch(`/users/${id}`).then(res => res.json());
  } catch (error) {
    throw new HttpException(`Could not fetch user ID "${id}", Error; ${error.message}`, error.status);
  }

  // Do something with user
}

With doublet

import axios from 'axios';
import doublet from 'doublet';
import HttpException from 'your-favorite-error-handler';

async function fetchUser(id: string): User {
  const {data: user, error: userError} = await doublet(axios, `/users/${id}`);
  if (userError) throw new HttpException(`Could not fetch user ID "${id}", Error; ${userError.message}`, userError.status);

  // Do something with user
}

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";
or

Import directly with a jsr specifier

import * as doublet from "jsr:@billyjacoby/doublet";

Add Package

pnpm i jsr:@billyjacoby/doublet
or (using pnpm 10.8 or older)
pnpm dlx jsr add @billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";

Add Package

yarn add jsr:@billyjacoby/doublet
or (using Yarn 4.8 or older)
yarn dlx jsr add @billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";

Add Package

vlt install jsr:@billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";

Add Package

npx jsr add @billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";

Add Package

bunx jsr add @billyjacoby/doublet

Import symbol

import * as doublet from "@billyjacoby/doublet";