Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Apr 24, 2024
1 parent ddc4d88 commit 4eacc5e
Showing 1 changed file with 45 additions and 58 deletions.
103 changes: 45 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
</a>
</p>

The set of async flow control structures and promise utils.

```sh
npm install --save-prod parallel-universe
```
Expand All @@ -16,7 +14,6 @@ npm install --save-prod parallel-universe
- [`Deferred`](#deferred)
- [`AsyncQueue`](#asyncqueue)
- [`WorkPool`](#workpool)
- [`Executor`](#executor)
- [`Lock`](#lock)
- [`Blocker`](#blocker)
- [`PubSub`](#pubsub)
Expand Down Expand Up @@ -202,61 +199,6 @@ pool.setSize(0);
// ⮕ Promise<void>
```

# `Executor`

Executor manages an async callback execution process and provides ways to access execution results, abort or replace an
execution, and subscribe to its state changes.

Create an `Executor` instance and submit a callback for execution:

```ts
const executor = new Executor();

executor.execute(doSomething);
// ⮕ AbortablePromise<void>
```

The `execute` method returns a promise that is fulfilled when the promise returned from the callback is settled. If
there's a pending execution, it is aborted and the new execution is started.

To abort the pending execution, you can use
an [abort signal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
passed to the executed callback:

```ts
executor.execute(async signal => {
// Check signal.aborted
});

executor.abort();
```

When execution is aborted the current `value` and `reason` remain intact.

To reset the executor to the initial state use:

```ts
executor.clear();
```

You can directly fulfill or reject an executor:

```ts
executor.resolve(value);

executor.reject(reason);
```

Subscribe to an executor to receive notifications when its state changes:

```ts
const unsubscribe = executor.subscribe(() => {
// Handle the update
});

unsubscribe();
```

# `Lock`

Promise-based [lock implementation](https://en.wikipedia.org/wiki/Lock_(computer_science)).
Expand Down Expand Up @@ -375,6 +317,51 @@ timeout(
);
```

# `retry`

Invokes a callback periodically until it successfully returns the result. If a callback throws an error or returns
a promise that is rejected then it is invoked again after a delay.

```ts
retry(async () => {
await doSomethingOrThrow();
});
// ⮕ AbortablePromise<void>
```

Specify a delay between tries:

```ts
retry(doSomethingOrThrow, 3000);
// ⮕ AbortablePromise<void>
```

Specify maximum number of tries:

```ts
retry(doSomethingOrThrow, 3000, 5);
// ⮕ AbortablePromise<void>
```

Abort the retry prematurely:

```ts
const promise = retry(doSomethingOrThrow, 3000);

promise.abort();
```

You can combine `retry` with [`timeout`](#timeout) to limit the retry duration:

```ts
timeout(
retry(async () => {
await doSomethingOrThrow();
}),
5000
);
```

# `waitFor`

Returns a promise that is fulfilled when a callback returns a truthy value:
Expand Down

0 comments on commit 4eacc5e

Please sign in to comment.