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 Future#get() and accept thunked promises in Future.from() #279

Merged
merged 2 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/alfa-future/src/future.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ export abstract class Future<T> implements Monad<T>, Functor<T> {
}
}

public get(): T {
let step: Future<T> = this;

while (true) {
const next = step.step();

if (step !== next) {
step = next;
} else {
return next.get();
}
}
}

public isNow(): boolean {
return this instanceof Now;
}
Expand All @@ -51,6 +65,8 @@ export abstract class Future<T> implements Monad<T>, Functor<T> {
}

export namespace Future {
export type Maybe<T> = T | Future<T>;

export function isFuture<T>(value: unknown): value is Future<T> {
return value instanceof Future;
}
Expand All @@ -71,8 +87,10 @@ export namespace Future {
return suspend(() => now(thunk()));
}

export function from<T>(promise: Promise<T>): Future<T> {
return Future.defer((callback) => promise.then(callback));
export function from<T>(promise: Promise<T> | Thunk<Promise<T>>): Future<T> {
return Future.defer((callback) =>
(typeof promise === "function" ? promise() : promise).then(callback)
);
}

export function traverse<T, U>(
Expand Down Expand Up @@ -116,6 +134,10 @@ class Now<T> extends Future<T> {
callback(this._value);
}

public get(): T {
return this._value;
}

public map<U>(mapper: Mapper<T, U>): Future<U> {
return new Now(mapper(this._value));
}
Expand Down Expand Up @@ -145,6 +167,10 @@ class Defer<T> extends Future<T> {
this._continuation((value) => defer(() => callback(value)));
}

public get(): never {
throw new Error("Attempted to .get() from deferred future");
}

public flatMap<U>(mapper: Mapper<T, Future<U>>): Future<U> {
return Defer.Bind.of(this._continuation, mapper);
}
Expand Down Expand Up @@ -181,6 +207,10 @@ namespace Defer {
);
}

public get(): never {
throw new Error("Attempted to .get() from deferred future");
}

public flatMap<U>(mapper: Mapper<T, Future<U>>): Future<U> {
return Suspend.of(() =>
Bind.of(this._continuation, (value) =>
Expand Down
16 changes: 16 additions & 0 deletions packages/alfa-future/test/future.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ test("#flatMap() does not overflow for long nested defer() chains", async (t) =>
t.equal(await n, 100000);
});

test("#get() returns the value of a non-deferred future", (t) => {
let n = Future.now(0);

for (let i = 0; i < 10; i++) {
n = n.flatMap((i) => Future.now(i + 1));
}

t.equal(n.get(), 10);
});

test(".traverse() traverses a list of values and lifts them to a future of lists", async (t) => {
t.deepEqual(
await Future.traverse([1, 2, 3, 4], (n) =>
Expand Down Expand Up @@ -154,3 +164,9 @@ test(".from() converts a promise to a future", async (t) => {

t.equal(await future, 2);
});

test(".from() converts a thunked promise to a future", async (t) => {
const future = Future.from(async () => 2);

t.equal(await future, 2);
});