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 TypeScript definition #31

Merged
merged 7 commits into from
Aug 20, 2018
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
37 changes: 37 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
interface ClearablePromise<T> extends Promise<T> {
/**
* Clears the delay and settles the promise.
*/
clear(): void;
}

declare const delay: {
/**
* Create a promise which resolves after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @returns A promise which resolves after the specified `milliseconds`.
*/
(milliseconds: number): ClearablePromise<void>;

/**
* Create a promise which resolves after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @param value - Value to resolve in the returned promise.
* @returns A promise which resolves after the specified `milliseconds`.
*/
<T>(milliseconds: number, value: T): ClearablePromise<T>;

/**
* Create a promise which rejects after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @param reason - Value to reject in the returned promise.
* @returns A promise which rejects after the specified `milliseconds`.
*/
// TODO: Allow providing reason type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved.
reject(milliseconds: number, reason?: any): ClearablePromise<never>;
};

export default delay;
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ const createDelay = willResolve => (ms, value) => {
return delayPromise;
};

module.exports = createDelay(true);
module.exports.reject = createDelay(false);
const delay = createDelay(true);
delay.reject = createDelay(false);
module.exports = delay;
module.exports.default = delay;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow such recursive

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"test": "xo && ava"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
Expand Down