[delay] introduce typings (#18526)

This commit is contained in:
Dimitri Benin
2017-07-31 16:25:53 -07:00
committed by Sheetal Nandi
parent 178dd15e5e
commit cfc549b2c1
4 changed files with 100 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
import delay = require('delay');
delay(200)
.then(() => {
});
let str: string;
let num: number;
Promise.resolve(delay(100))
.then(() => {
});
Promise.resolve("foo")
.then(delay<string>(100))
.then(result => {
str = result;
});
Promise.resolve('foo')
.then(delay(100, 10))
.then(result => {
num = result;
});
Promise.resolve('foo')
.then(delay.reject(100))
.then(x => 10)
.catch(err => {
});
Promise.resolve('foo')
.then(delay.reject(100, 'bar'))
.then(x => 10)
.catch(err => {
});
(async () => {
const delaying = delay(1000);
delaying.cancel();
try {
await delaying;
} catch (err) {
// `err` is an instance of `delay.CancelError`
}
})();
throw new delay.CancelError('bar');
+30
View File
@@ -0,0 +1,30 @@
// Type definitions for delay 2.0
// Project: https://github.com/sindresorhus/delay#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
export = delay;
declare function delay<T>(ms: number): delay.PDelayedPassThroughThunk<T>;
declare function delay<T>(ms: number, value: T): delay.PDelayedThunk<T>;
declare namespace delay {
function reject(ms: number, rejectionValue?: any): PDelayedThunk<never>;
class CancelError extends Error {
readonly name: 'CancelError';
constructor(message?: string);
}
type PDelayedThunk<T> = ((value: any) => DelayedPromiseLike<T>) & DelayedPromiseLike<T>;
type PDelayedPassThroughThunk<TValue> = ((value: TValue) => DelayedPromiseLike<TValue>) & DelayedPromiseLike<void>;
interface DelayedPromiseLike<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
cancel(): void;
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"delay-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }