Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isFulfilled and isRejected guards for use with Promise.allSettled #25

Merged
merged 4 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-games-try.md
@@ -0,0 +1,5 @@
---
'emery': minor
---

Added `isFulfilled` and `isRejected` guards for use with `Promise.allSettled`
30 changes: 30 additions & 0 deletions docs/pages/docs/guards.md
Expand Up @@ -98,3 +98,33 @@ Checks whether an array is **not** empty.
```ts
function isNonEmptyArray<T>(value: T[]): value is [T, ...T[]];
```

## Promise

Guards for [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) related types.

### isFulfilled

Checks whether a result from [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) is fulfilled

```ts
function isFulfilled<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T>;
```

```ts
const results = await Promise.allSettled(promises);
const fulfilledValues = results.filter(isFulfilled).map(result => result.value);
```

### isRejected

Checks whether a result from [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) is rejected.

```ts
function isRejected(result: PromiseSettledResult<unknown>): result is PromiseRejectedResult;
```

```ts
const results = await Promise.allSettled(promises);
const rejectionReasons = results.filter(isRejected).map(result => result.reason);
```
15 changes: 15 additions & 0 deletions src/guards.test.ts
@@ -1,10 +1,12 @@
import {
isBoolean,
isDefined,
isFulfilled,
isNonEmptyArray,
isNull,
isNullish,
isNumber,
isRejected,
isString,
isUndefined,
} from './guards';
Expand Down Expand Up @@ -85,4 +87,17 @@ describe('guards', () => {
});
});
});

describe('promise', () => {
it('isFulfilled should validate assumed values', async () => {
expect(
(await Promise.allSettled([Promise.resolve(), Promise.reject()])).map(x => isFulfilled(x)),
).toEqual([true, false]);
});
it('isRejected should validate assumed values', async () => {
expect(
(await Promise.allSettled([Promise.resolve(), Promise.reject()])).map(x => isRejected(x)),
).toEqual([false, true]);
});
});
});
30 changes: 30 additions & 0 deletions src/guards.ts
@@ -1,3 +1,4 @@
/// <reference lib="es2020.promise" />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to automatically include the types used if someone isn't already including the lib file for it.

import { Nullish } from './types';

// Primitives
Expand Down Expand Up @@ -48,3 +49,32 @@ export function isNullish(value: unknown): value is Nullish {
export function isDefined<T>(value: T): value is NonNullable<T> {
return !isNullish(value);
}

// Promise
// ------------------------------

/**
* Checks whether a result from `Promise.allSettled` is fulfilled
*
* ```ts
* const results = await Promise.allSettled(promises);
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value);
* ```
*/
export function isFulfilled<T>(
result: PromiseSettledResult<T>,
): result is PromiseFulfilledResult<T> {
return result.status === 'fulfilled';
}

/**
* Checks whether a result from `Promise.allSettled` is rejected
*
* ```ts
* const results = await Promise.allSettled(promises);
* const rejectionReasons = results.filter(isRejected).map(result => result.reason);
* ```
*/
export function isRejected(result: PromiseSettledResult<unknown>): result is PromiseRejectedResult {
return result.status === 'rejected';
}
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2016",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
Expand Down