implement random time
This commit is contained in:
parent
645a1ca111
commit
62a5975fb3
4
dist/index.d.ts
vendored
4
dist/index.d.ts
vendored
@ -5,6 +5,10 @@ import 'typings-global';
|
||||
* @param passOn
|
||||
*/
|
||||
export declare let delayFor: <T>(timeInMillisecond: number, passOn?: T) => Promise<T>;
|
||||
/**
|
||||
* delay for a random time
|
||||
*/
|
||||
export declare let delayForRandom: <T>(timeMinInMillisecond: number, timeMaxInMillisecond: number, passOn?: T) => Promise<T>;
|
||||
export declare class Timeout<T> {
|
||||
promise: Promise<T>;
|
||||
private _deferred;
|
||||
|
13
dist/index.js
vendored
13
dist/index.js
vendored
@ -23,6 +23,17 @@ exports.delayFor = (timeInMillisecond, passOn) => __awaiter(this, void 0, void 0
|
||||
});
|
||||
return passOn;
|
||||
});
|
||||
/**
|
||||
* delay for a random time
|
||||
*/
|
||||
exports.delayForRandom = (timeMinInMillisecond, timeMaxInMillisecond, passOn) => __awaiter(this, void 0, void 0, function* () {
|
||||
yield new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
|
||||
});
|
||||
return passOn;
|
||||
});
|
||||
class Timeout {
|
||||
constructor(timeInMillisecondArg, passOn) {
|
||||
this._cancelled = false;
|
||||
@ -43,4 +54,4 @@ class Timeout {
|
||||
}
|
||||
}
|
||||
exports.Timeout = Timeout;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsMEJBQXVCO0FBQ3ZCLGlDQUFnQztBQUVoQzs7OztHQUlHO0FBQ1EsUUFBQSxRQUFRLEdBQUcsQ0FBVSxpQkFBeUIsRUFBRSxNQUFVO0lBQ25FLE1BQU0sSU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QUE7UUFDbEMsSUFBSSxDQUFDLE9BQU8sR0FBRyxJQUFJLENBQUMsU0FBUyxDQUFDLE9BQU8sQ0FBQTtRQUNyQyxJQUFJLENBQUMsUUFBUSxHQUFHLFVBQVUsQ0FBQyxHQUFHLEVBQUU7WUFDOUIsRUFBRSxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztnQkFDckIsSUFBSSxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUMsTUFBTSxDQUFDLENBQUE7WUFDaEMsQ0FBQztRQUNILENBQUMsRUFBRSxvQkFBb0IsQ0FBQyxDQUFBO0lBQzFCLENBQUM7SUFFRCxXQUFXO1FBQ1QsSUFBSSxDQUFDLFFBQVEsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUN2QixDQUFDO0lBRUQsTUFBTTtRQUNKLElBQUksQ0FBQyxVQUFVLEdBQUcsSUFBSSxDQUFBO1FBQ3RCLElBQUksQ0FBQyxXQUFXLEVBQUUsQ0FBQTtJQUNwQixDQUFDO0NBQ0Y7QUF2QkQsMEJBdUJDIn0=
|
14
test/test.ts
14
test/test.ts
@ -1,6 +1,6 @@
|
||||
import { expect, tap } from 'tapbundle'
|
||||
|
||||
import * as smartdelay from '../dist/index'
|
||||
import * as smartdelay from '../ts/index'
|
||||
|
||||
tap.test('.delayFor should delay async', async (tools) => {
|
||||
tools.timeout(5000)
|
||||
@ -14,6 +14,18 @@ tap.test('.delayFor should delay async', async (tools) => {
|
||||
})
|
||||
})
|
||||
|
||||
tap.test('.delayForRandpm should delay async for a random time period', async (tools) => {
|
||||
tools.timeout(5000)
|
||||
let timePassed = false
|
||||
setTimeout(() => {
|
||||
timePassed = true
|
||||
}, 3000)
|
||||
await smartdelay.delayForRandom(3000,4900).then(async () => {
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(timePassed).to.be.true
|
||||
})
|
||||
})
|
||||
|
||||
tap.test('.delayFor should pass on a type', async (tools) => {
|
||||
tools.timeout(5000)
|
||||
let timePassed = false
|
||||
|
15
ts/index.ts
15
ts/index.ts
@ -18,6 +18,21 @@ export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
||||
return passOn
|
||||
}
|
||||
|
||||
/**
|
||||
* delay for a random time
|
||||
*/
|
||||
export let delayForRandom = async <T>(timeMinInMillisecond: number, timeMaxInMillisecond: number, passOn?: T) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
setTimeout(
|
||||
() => {
|
||||
resolve()
|
||||
},
|
||||
Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond
|
||||
)
|
||||
})
|
||||
return passOn
|
||||
}
|
||||
|
||||
export class Timeout<T> {
|
||||
promise: Promise<T>
|
||||
private _deferred: smartq.Deferred<T>
|
||||
|
Loading…
Reference in New Issue
Block a user