Skip to content

Commit d9b0122

Browse files
committed
docs
1 parent 2df3d63 commit d9b0122

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

Diff for: README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# promise-or-value
2+
3+
[![Actions Status](https://github.com/descriptinc/promise-or-value/workflows/Node%20CI/badge.svg)](https://github.com/descriptinc/promise-or-value/actions)
4+
[![npm version](https://badge.fury.io/js/promise-or-value.svg)](https://badge.fury.io/js/promise-or-value)
5+
[![codecov](https://codecov.io/gh/descriptinc/promise-or-value/branch/master/graph/badge.svg)](https://codecov.io/gh/descriptinc/promise-or-value)
6+
7+
This module is designed around the idea of a custom TypeScript type:
8+
9+
```typescript
10+
type PromiseOrValue<T> = Promise<T> | T;
11+
```
12+
13+
The motivations for this type is because `Promise`s resolve asynchronously, even if you call `then` on an already-
14+
resolved promise (e.g. `Promise.resolve(5).then(() => …)`).
15+
16+
Promises work this way by design! It avoids bugs.
17+
18+
But for performance-critical code where you're working with cacheable async data in a loop, it can be too slow.
19+
20+
## API
21+
22+
### `then(pov, onValue, onError)` - "fast" equivalent of `Promise.resolve(pov).then(onValue).catch(onError)`
23+
24+
```typescript
25+
export function then<T, V>(
26+
value: PromiseOrValue<T>,
27+
onValue: (t: T, sync?: true) => PromiseOrValue<V>,
28+
onError: (error: any) => PromiseOrValue<V> = throwingErrorCallback,
29+
): PromiseOrValue<V>;
30+
```
31+
32+
Run some logic immediately on values, or later, for promises.
33+
34+
### `all(povs)` - "fast" equivalent of `Promise.all(povs)`
35+
36+
```typescript
37+
export function all<T>(values: PromiseOrValue<T>[]): PromiseOrValue<T[]>;
38+
```
39+
40+
Returns array as-is if everything is a value, or calls `Promise.all` if there are any promises in the array
41+
42+
### `PromiseOrValueMapLike` - for caching promise data
43+
44+
```typescript
45+
type PromiseOrValueMapLike<K, V> = {
46+
get(key: K): PromiseOrValue<V> | undefined;
47+
set(key: K, value: PromiseOrValue<V>): void;
48+
};
49+
```
50+
51+
A type used by `getOrAdd`. A simple conforming cache can be made with `new Map<K, PromiseOrValue<V>>()`.
52+
53+
### `getOrAdd` - for working with `PromiseOrValueMapLike`
54+
55+
```typescript
56+
export function getOrAdd<K, V>(
57+
cache: PromiseOrValueMapLike<K, V>,
58+
key: K,
59+
compute: (key: K) => PromiseOrValue<V>,
60+
): PromiseOrValue<V>;
61+
```
62+
63+
If the key exists in the cache, returns it, otherwise computes it and inserts it in the cache. If the computation
64+
returns a promise, the promise is inserted in the cache, and replaced with the literal value once it resolves.

Diff for: src/then.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const throwingErrorCallback = (e: any) => {
1414

1515
export function then<T, V>(
1616
value: PromiseOrValue<T>,
17-
callback: (t: T, sync?: true) => PromiseOrValue<V>,
18-
errorCallback: (error: any) => PromiseOrValue<V> = throwingErrorCallback,
17+
onValue: (t: T, sync?: true) => PromiseOrValue<V>,
18+
onError: (error: any) => PromiseOrValue<V> = throwingErrorCallback,
1919
): PromiseOrValue<V> {
2020
if (value instanceof Promise) {
21-
return value.then(callback).catch(errorCallback);
21+
return value.then(onValue).catch(onError);
2222
}
2323
try {
24-
return callback(value, true);
24+
return onValue(value, true);
2525
} catch (e) {
26-
return errorCallback(e);
26+
return onError(e);
2727
}
2828
}

0 commit comments

Comments
 (0)