-
Notifications
You must be signed in to change notification settings - Fork 621
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
Redesign of http server module #188
Changes from 2 commits
ea7c952
0a2c55e
ede2fff
ea4dff1
74d3d45
e6e31ef
d210a53
326f199
6a1446a
c084a62
92cdb03
8b131d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
export type Deferred<T = any, R = Error> = { | ||
promise: Promise<T>; | ||
resolve: (t?: T) => void; | ||
reject: (r?: R) => void; | ||
}; | ||
|
||
/** Create deferred promise that can be resolved and rejected by outside */ | ||
export function defer<T>(): Deferred<T> { | ||
let resolve = () => {}; | ||
let reject = () => {}; | ||
const promise = new Promise<T>((res, rej) => { | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
return { promise, resolve, reject }; | ||
} | ||
|
||
export function isDeferred(x): x is Deferred { | ||
return ( | ||
typeof x === "object" && | ||
x.promise instanceof Promise && | ||
typeof x["resolve"] === "function" && | ||
typeof x["reject"] === "function" | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this testing the same thing as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's custom type guard for |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assert, test } from "../testing/mod.ts"; | ||
import { defer, isDeferred } from "./deferred.ts"; | ||
|
||
test(async function asyncIsDeferred() { | ||
const d = defer(); | ||
assert.assert(isDeferred(d)); | ||
assert.assert( | ||
isDeferred({ | ||
promise: null, | ||
resolve: () => {}, | ||
reject: () => {} | ||
}) === false | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know about
/async
... seems a bit too specific.I feel like
/util
would be a useful junk drawer for one-off things like this.@hayd what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also considered to let it
promise
FYI. I don't have any special reason toasync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename this to
//util/deferred.ts
?