Skip to content

Commit

Permalink
chore: update readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlden committed Jan 23, 2023
1 parent bd59e6e commit f4d25e8
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 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...
Expand All @@ -7,70 +8,47 @@ This module attempts to solve that nesting hell and instead provide clean and fl

<br>

### Installation _(pick one)_
- `npm i @dvlden/await-it`
- `pnpm i @dvlden/await-it`
- `yarn add @dvlden/await-it`
## Installation

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

```bash
pnpm i @dvlden/await-it
```

<br>

### Usage

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

```js
```ts
// Fake Global Promises
const fakeResolve = Promise.resolve({ name: 'Nenad Novakovic', age: 28 })
const fakeReject = Promise.reject(new Error('Rejection reason...'))
```

<br>

> Without TypeScript
```js
import { it } from '@dvlden/await-it'

(async () => {
const [res, err] = await it(fakeResolve);

console.log(res) // { name: 'Nenad Novakovic', age: 28 }
console.log(err) // null
})()

(async () => {
const [res, err] = await it(fakeReject);

console.log(res) // undefined
console.log(err.message) // Rejection reason...
})
```

<br>

> With TypeScript
```ts
import { it } from '@dvlden/await-it'

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

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

console.log(res) // { name: 'Nenad Novakovic', age: 28 }
console.log(res.name) // Nenad Novakovic
})()
```

<br>

> Node Environment
```js
// Import as CommonJS
const { it } = require('@dvlden/await-it')

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

0 comments on commit f4d25e8

Please sign in to comment.