Skip to content

Commit

Permalink
Add TypeScript definition (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-korolev authored and sindresorhus committed Aug 20, 2018
1 parent 2e202e5 commit 3e383c6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
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;
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

0 comments on commit 3e383c6

Please sign in to comment.