Skip to content

Commit

Permalink
Update the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kossidts committed Mar 27, 2024
1 parent 5a9debb commit 1f3688f
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

[![License][license-image]][license-url] [![NPM Package Version][npm-image-version]][npm-url] ![GitHub top language][language-image] ![Size][size-image] ![Last Commit][commit-image] ![Workflow CI][workflow-image]

A simple to use (async await) wrapper to resolve functions, promises and more.
An easy-to-use **async/await wrapper** to resolve functions, promises, and any asynchronous task.

It provides a timeout functionalitiy to delay/timeout execution.
**`await-resolver` always returns an array of type `[error, result]` ensuring consistent error handling across your code. You no longer have to use try/catch blocks or callbacks** if you don't want to.

The resolver always returns an array of type `[error, result]` to ease error handling and eliminate the need to (try-)catch.
Additionally `await-resolver` provides a **timeout functionality** to delay/timeout an execution. You can use it as a **sleep** function.

Syntax:

Expand All @@ -16,6 +16,8 @@ Syntax:

`await resolver(fn, timeout, fn_param1, fn_param2, ..., fn_paramN)`

`await resolver.sleep(timeout)`

Or as a promise

`resolver(something).then(([result, error]) => { ... })`
Expand All @@ -24,10 +26,12 @@ Or as a promise

`resolver(fn, timeout, fn_param1, fn_param2, ..., fn_paramN).then(([result, error]) => { ... })`

`resolver.sleep(timeout).then(([result, error]) => { ... })`

## Installation

```bash
$ npm i await-resolver
$ npm install await-resolver
```

## Usage
Expand All @@ -50,29 +54,45 @@ import resolver from "await-resolver";
import { resolver } from "await-resolver";
```

#### Await the resolver inside an async function
#### Await the resolver

```js
async () => {
let anything; // can be a function, a promise, an error, or any kind of value
let [error, result] = await resolver(anything);
// If you environment does not support top-level await,
// wrap everything in an async function.

// await-resolve a fetch result
const fetchRequest = fetch(urlOrRequest[, options]);

// in one go
let [error, data] = await resolver(fetchRequest.then(res => res.json()));

// or step by step
let [responseError, response] = await resolver(fetchRequest);
if(responseError){
// return early
}
let [dataError, data] = await resolver(response.json());

// Use timeout to delay execution
let timeout = 1500; // ms just like setTimeout

// just sleep
await resolver(null, timeout);

// Resolve a function without arguments, with timeout
let [error, result] = await resolver(my_function, timeout);
// await-resolve anything: a function, a promise, an error, or any kind of value
let [error, result] = await resolver(anything);

// Resolve a function with arguments, without timeout
let multiply = (a, b) => a * b;
let [error, result] = await resolver(multiply, null, 4, 5); // => [null, 20]
// Use timeout to delay execution
let timeout = 1500; // ms just like setTimeout

// just sleep
await resolver(null, timeout);

// Resolve a function without arguments, with timeout
let [error, result] = await resolver(my_function, timeout);

// Resolve a function with arguments, without timeout
let multiply = (a, b) => a * b;
let [error, result] = await resolver(multiply, null, 4, 5); // => [null, 20]

// Resolve a function with arguments, with timeout
let [error, result] = await resolver(multiply, timeout, 4, 5); // => [null, 20] after timeout
};
// Resolve a function with arguments, with timeout
let [error, result] = await resolver(multiply, timeout, 4, 5); // => [null, 20] after timeout
```

#### Or use the resolver as a regular promise
Expand Down

0 comments on commit 1f3688f

Please sign in to comment.