Skip to content

Commit

Permalink
feat(settlement): create types representing the result of a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertFischer committed Dec 10, 2020
1 parent 823a8d7 commit 269075e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @format */

import { Fulfillment, Rejection } from "./types";
import _ from "lodash";

describe("Fulfillment", () => {
_.forEach([true, { foo: true }], (arg) => {
describe(`when passed the ${typeof arg} argument '${JSON.stringify(
arg
)}'`, () => {
const fulfillment = new Fulfillment(arg);

it("exposes the argument on the 'value' property", () => {
expect(fulfillment).toHaveProperty("value", arg);
});

it("has a 'status' property of 'fulfilled'", () => {
expect(fulfillment).toHaveProperty("status", "fulfilled");
});
});
});
});

describe("Rejection", () => {
_.forEach(["BOOM!", new Error("BOOM!")], (arg) => {
describe(`when passed the ${typeof arg} argument: '${arg}'`, () => {
const rejection = new Rejection(arg);

it("returns the argument in the 'reason' property", () => {
expect(rejection).toHaveProperty("reason", arg);
});

it("has a 'status' property whose value is 'rejected'", () => {
expect(rejection).toHaveProperty("status", "rejected");
});
});
});
});
27 changes: 27 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,30 @@ export type PromisableIterable<T> = Promisable<IterableOfPromisables<T>>;
* although it may produce either values or promises of values or both.
*/
export type IterableOfPromisables<T> = Iterable<Promisable<T>>;

/**
* Represents a `PromiseSettledResult` value for a fulfilled promise.
*/
export class Fulfillment<T> implements PromiseFulfilledResult<T> {
constructor(public readonly value: T) {}

get status(): "fulfilled" {
return "fulfilled";
}
}

/**
* Represents a `PromiseSettledResult` value for a rejected promise.
*/
export class Rejection implements PromiseRejectedResult {
constructor(public readonly reason: unknown) {}

get status(): "rejected" {
return "rejected";
}
}

/**
* Equivalent to a `PromiseSettledResult`, but specific to our classes.
*/
export type Settlement<T> = Fulfillment<T> | Rejection;

0 comments on commit 269075e

Please sign in to comment.