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

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

merged 2 commits into from
Jul 22, 2021

Conversation

lowlighter
Copy link
Contributor

This edits a bit deferred<T> in order to be able to track promise status, which is not exposed directly by default.
It's mostly useful to know whether the deferred promise is still pending or not (as they're manually resolved)

Personnaly I sometimes use it in unit testing so I figured out it may be useful for others too, though I could understand it may not be that revelant 🙂

@kt3k
Copy link
Member

kt3k commented Jul 21, 2021

I like the idea. Because deferred is mostly used for testing purpose, this addition makes sense to me.

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 👍

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

@lowlighter LGTM. Thank you for the contribution!

@kt3k kt3k merged commit 3ea4939 into denoland:main Jul 22, 2021
@@ -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.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 22, 2021

Also, just to mention, why wouldn't we want to use Object.defineProperty() and make the value read only? No big deal though since this is already merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants