diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..0fcb115 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,37 @@ +interface ClearablePromise extends Promise { + /** + * 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; + + /** + * 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`. + */ + (milliseconds: number, value: T): ClearablePromise; + + /** + * 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; +}; + +export default delay; diff --git a/index.js b/index.js index 905c856..883da08 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index 5e56991..febebb0 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "test": "xo && ava" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "promise",