@dvlden/await-it
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Await it

A wrapper for async/await calls without the need of try/catch block.

If someone told you that you should try / catch once at the root of your application and then do your async calls anywhere in the codebase, they are wrong. You should try / catch every async / await call or use .then() / .catch() chainables. Better than callback hell? I think it leads to similar problem...

This module attempts to solve that nesting hell and instead provide clean and fluent api with tuple destructuring.


Installation

Use your favourite package manager... In my case that's pnpm.

pnpm i @dvlden/await-it

Usage

We'll pretend that we have some kind of Promise already declared globally, to avoid bulky README file.

// Fake Global Promises
const fakeResolve = Promise.resolve({ name: 'Nenad Novakovic', age: 28 })
const fakeReject = Promise.reject(new Error('Rejection reason...'))
import { it } from '@dvlden/await-it'

;(async () => {
  interface User {
    name: string
    age: number
  }

  const [res] = await it<User>(fakeResolve)

  console.log(res.name) // Nenad Novakovic
})()

// Import as CommonJS
const { it } = require('@dvlden/await-it')

// Import as ESM
import { it } from '@dvlden/await-it'

Package Sidebar

Install

npm i @dvlden/await-it

Weekly Downloads

0

Version

1.0.6

License

MIT

Unpacked Size

4.12 kB

Total Files

7

Last publish

Collaborators

  • dvlden