https://github.com/jellydn/better-async-demo
Easy to handle the async await without try catch
https://github.com/jellydn/better-async-demo
async-await js typescript
Last synced: about 2 months ago
JSON representation
Easy to handle the async await without try catch
- Host: GitHub
- URL: https://github.com/jellydn/better-async-demo
- Owner: jellydn
- Created: 2023-02-05T07:20:49.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-22T14:06:58.000Z (11 months ago)
- Last Synced: 2024-05-22T15:31:45.300Z (11 months ago)
- Topics: async-await, js, typescript
- Language: TypeScript
- Homepage: https://www.youtube.com/watch?v=iLB75jzQrJ8
- Size: 43 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Welcome to better-async 👋
> Easy to handle the async await without try catch
[![IT Man - Tech #32 - Async await wrapper for easy error handling without try-catch [Vietnamese]](https://i.ytimg.com/vi/iLB75jzQrJ8/hqdefault.jpg)](https://www.youtube.com/watch?v=iLB75jzQrJ8)
## Prerequisites
- [Bun — fast all-in-one JavaScript runtime](https://bun.sh/)
## Libraries
- [scopsy/await-to-js: Async await wrapper for easy error handling without try-catch](https://github.com/scopsy/await-to-js)
## Install
```sh
bun install
```## Usage
This is simple usage for await-to-js with Bun.
```typescript
// 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(p);
if (err) console.error(err);
console.log(data?.test);
// Output: 123const [err2] = await to(pError);
if (err2) console.error(err2);// Output: Error: Test error
``````sh
❯ 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**
- Website: https://productsway.com/
- Twitter: [@jellydn](https://twitter.com/jellydn)
- Github: [@jellydn](https://github.com/jellydn)## Show your support
[](https://ko-fi.com/dunghd)
[](https://paypal.me/dunghd)
[](https://www.buymeacoffee.com/dunghd)Give a ⭐️ if this project helped you!