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

feat(async): add status to deferred promises #1047

Merged
merged 2 commits into from
Jul 22, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions async/deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// See https://github.com/Microsoft/TypeScript/issues/15202
// At the time of writing, the github issue is closed but the problem remains.
export interface Deferred<T> extends Promise<T> {
status: "pending" | "fulfilled" | "rejected";
Copy link
Contributor

Choose a reason for hiding this comment

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

In the ECMAScript specification, the internal field is called [[PromiseState]], so I think state would be better name.

resolve(value?: T | PromiseLike<T>): void;
// deno-lint-ignore no-explicit-any
reject(reason?: any): void;
Expand All @@ -20,7 +21,18 @@ export interface Deferred<T> extends Promise<T> {
export function deferred<T>(): Deferred<T> {
let methods;
const promise = new Promise<T>((resolve, reject): void => {
methods = { resolve, reject };
methods = {
async resolve(value: T | PromiseLike<T>) {
await value;
Object.assign(promise, { status: "fulfilled" });
lowlighter marked this conversation as resolved.
Show resolved Hide resolved
resolve(value);
},
// deno-lint-ignore no-explicit-any
reject(reason?: any) {
Object.assign(promise, { status: "rejected" });
reject(reason);
},
};
});
return Object.assign(promise, methods) as Deferred<T>;
return Object.assign(promise, methods, { status: "pending" }) as Deferred<T>;
}
15 changes: 15 additions & 0 deletions async/deferred_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ import { deferred } from "./deferred.ts";

Deno.test("[async] deferred: resolve", async function () {
const d = deferred<string>();
assertEquals(d.status, "pending");
d.resolve("🦕");
assertEquals(await d, "🦕");
assertEquals(d.status, "fulfilled");
});

Deno.test("[async] deferred: reject", async function () {
const d = deferred<number>();
assertEquals(d.status, "pending");
d.reject(new Error("A deno error 🦕"));
await assertThrowsAsync(async () => {
await d;
});
assertEquals(d.status, "rejected");
});

Deno.test("[async] deferred: status with promised value", async function () {
const d = deferred<string>();
const e = deferred<string>();
assertEquals(d.status, "pending");
d.resolve(e);
assertEquals(d.status, "pending");
Copy link
Member

Choose a reason for hiding this comment

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

nice 👍

e.resolve("🦕");
assertEquals(await d, "🦕");
assertEquals(d.status, "fulfilled");
});