Skip to content

Commit

Permalink
Add isFulfilled and isRejected guards for use with Promise.allSettled (
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Jun 24, 2022
1 parent 175efc4 commit cec7317
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rude-games-try.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference lib="es2020.promise" />
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2016",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
Expand Down

1 comment on commit cec7317

@vercel
Copy link

@vercel vercel bot commented on cec7317 Jun 24, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

emery – ./

emery-git-main-thinkmill.vercel.app
emery-thinkmill.vercel.app
emery-ts.vercel.app

Please sign in to comment.