Skip to content

jellydn/better-async-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to better-async 👋

Version

Easy to handle the async await without try catch

IT Man - Tech #32 - Async await wrapper for easy error handling without try-catch [Vietnamese]

Prerequisites

Libraries

Install

bun install

Usage

This is simple usage for await-to-js with Bun.

// index.ts
import { to } from "await-to-js";

interface ServerResponse {
  test: number;
}

const p = Promise.resolve({ test: 123 });
const pError = Promise.reject(new Error("Test error"));

const [err, data] = await to<ServerResponse>(p);
if (err) console.error(err);
console.log(data?.test);
// Output: 123

const [err2] = await to<ServerResponse>(pError);
if (err2) console.error(err2);

// Output: Error: Test error
❯ bun run index.ts
123
3 | interface ServerResponse {
4 |   test: number;
5 | }
6 |
7 | const p = Promise.resolve({ test: 123 });
8 | const pError = Promise.reject(new Error("Test error"));
                                 ^
error: Test error
      at /Users/huynhducdung/Projects/research/better-async/index.ts:8:30

Author

👤 Huynh Duc Dung

Show your support

kofi paypal buymeacoffee

Give a ⭐️ if this project helped you!