Skip to content

Commit

Permalink
Merge pull request #67 from swan-io/prevent-future-hanging-when-circular
Browse files Browse the repository at this point in the history
Prevent Futures from hanging when circular
  • Loading branch information
bloodyowl authored Feb 20, 2024
2 parents 29c3396 + 8f52f4e commit dfe94fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Future.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export class Future<A> {
) {
const resolver = (value: A) => {
if (this._state.tag === "Pending") {
this._state.resolveCallbacks?.forEach((func) => func(value));
const resolveCallbacks = this._state.resolveCallbacks;
this._state = { tag: "Resolved", value };
resolveCallbacks?.forEach((func) => func(value));
}
};

Expand Down
10 changes: 10 additions & 0 deletions test/Future.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,13 @@ test("Future isFuture", async () => {
expect(Future.isFuture([])).toEqual(false);
expect(Future.isFuture({})).toEqual(false);
});

test("Future doesn't hang", async () => {
const future = Future.make((resolve) => {
setTimeout(() => resolve(0), 10);
});
return future
.flatMap(() => Future.value(1))
.flatMap(() => future.flatMap(() => Future.value(2)))
.tap((value) => expect(value).toEqual(2));
});

0 comments on commit dfe94fb

Please sign in to comment.