Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa2d748

Browse filesBrowse files
committed
await
1 parent 2f75385 commit aa2d748
Copy full SHA for aa2d748

16 files changed

+95
-26
lines changed

‎lib/functions/delay.d.ts

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
/**
2+
* Delayed resolving promise
3+
* @param time Time in ms before promise will resolved
4+
* @param value Value to be returned in Promise.resolve
5+
* @returns Promise
6+
*/
17
export declare function delay(time: number, value?: any): Promise<any>;

‎lib/functions/delay.js

Copy file name to clipboardExpand all lines: lib/functions/delay.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
"use strict";
2+
/**
3+
* Delayed resolving promise
4+
* @param time Time in ms before promise will resolved
5+
* @param value Value to be returned in Promise.resolve
6+
* @returns Promise
7+
*/
28
function delay(time, value) {
39
return new Promise(function (resolve) {
410
setTimeout(resolve.bind(null, value), time);

‎lib/functions/retry.d.ts

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import { ITask } from "../types";
2-
export declare function retry(task: ITask, times: number): Promise<any>;
2+
export declare function retry(task: ITask, times?: number, delay?: number): Promise<any>;

‎lib/functions/retry.js

Copy file name to clipboard
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
"use strict";
2-
function process(task, times, reasons, resolve, reject) {
3-
task().then(function (result) {
2+
var prow = require("../prow");
3+
function process(task, times, reasons, delay, resolve, reject) {
4+
return task().then(function (result) {
45
resolve(result);
56
}).catch(function (reason) {
67
reasons.push(reason);
7-
if (reasons.length >= times) {
8+
if (reasons.length >= times && times >= 0) {
89
reject(reasons);
910
}
1011
else {
11-
process(task, times, reasons, resolve, reject);
12+
if (delay > 0) {
13+
prow.delay(delay).then(function () { return process(task, times, reasons, delay, resolve, reject); });
14+
}
15+
else {
16+
process(task, times, reasons, delay, resolve, reject);
17+
}
1218
}
1319
});
1420
}
15-
function retry(task, times) {
21+
function retry(task, times, delay) {
22+
if (times === void 0) { times = -1; }
23+
if (delay === void 0) { delay = 0; }
1624
return new Promise(function (resolve, reject) {
1725
var reasons = [];
18-
process(task, times, reasons, resolve, reject);
26+
process(task, times, reasons, delay, resolve, reject);
1927
});
2028
}
2129
exports.retry = retry;

‎lib/functions/timeout.d.ts

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import { ITask } from "../types";
2-
export declare function timeout(time: number, task: ITask): Promise<any>;
2+
export declare function timeout(task: ITask, time: number): Promise<any>;

‎lib/functions/timeout.js

Copy file name to clipboardExpand all lines: lib/functions/timeout.js
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"use strict";
22
var types_1 = require("../types");
3-
function timeout(time, task) {
3+
function timeout(task, time) {
44
return new Promise(function (resolve, reject) {
5-
var timeout = setTimeout(reject.bind(null, new types_1.TimeoutError()), time);
5+
var timeout = -1;
6+
if (time >= 0) {
7+
timeout = setTimeout(reject.bind(null, new types_1.TimeoutError()), time);
8+
}
69
task().then(function (result) {
710
resolve(result);
811
clearTimeout(timeout);

‎lib/prow.d.ts

Copy file name to clipboardExpand all lines: lib/prow.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { waterfall } from "./functions/waterfall";
55
export { retry } from "./functions/retry";
66
export { times } from "./functions/times";
77
export { parallel, queue } from "./functions/parallel";
8+
export { await } from "./functions/await";

‎lib/prow.js

Copy file name to clipboardExpand all lines: lib/prow.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ exports.times = times_1.times;
1414
var parallel_1 = require("./functions/parallel");
1515
exports.parallel = parallel_1.parallel;
1616
exports.queue = parallel_1.queue;
17+
var await_1 = require("./functions/await");
18+
exports.await = await_1.await;

‎lib/types.d.ts

Copy file name to clipboardExpand all lines: lib/types.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="node" />
12
export interface ITask {
23
(...args: any[]): Promise<any>;
34
}

‎src/functions/await.ts

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
ITask
3+
} from "../types";
4+
5+
import * as prow from "../prow";
6+
7+
/**
8+
* Returns an Promise which will resolve when the condition is satisfied, or rejected if timeout expired
9+
* @param condition Task which should resolve with check result
10+
* @param delay Delay between when condition task return value and run new one
11+
* @param timeout Timeout before promise will rejected. `-1` for endless waiting.
12+
* @returns Promise
13+
*/
14+
export function await(condition: ITask, delay: number, timeout: number = -1): Promise<any> {
15+
const promise = new Promise(function (resolve) {
16+
const conditionHandler = (result) => {
17+
if (result) {
18+
resolve();
19+
}
20+
};
21+
prow.retry(condition, -1, delay).then(conditionHandler);
22+
});
23+
24+
return prow.timeout(() => promise, timeout);
25+
}

‎src/functions/delay.ts

Copy file name to clipboardExpand all lines: src/functions/delay.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Delayed resolving promise
3+
* @param time Time in ms before promise will resolved
4+
* @param value Value to be returned in Promise.resolve
5+
* @returns Promise
6+
*/
17
export function delay(time: number, value?: any): Promise<any> {
28
return new Promise(function (resolve) {
39
setTimeout(resolve.bind(null, value), time);

‎src/functions/retry.ts

Copy file name to clipboardExpand all lines: src/functions/retry.ts
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ import {
22
ITask
33
} from "../types";
44

5-
function process(task: ITask, times: number, reasons: any[], resolve, reject) {
6-
task().then((result) => {
5+
import * as prow from "../prow";
6+
7+
function process(task: ITask, times: number, reasons: any[], delay, resolve, reject): Promise<any> {
8+
return task().then((result) => {
79
resolve(result);
810
}).catch((reason) => {
911
reasons.push(reason);
10-
if (reasons.length >= times) {
12+
if (reasons.length >= times && times >= 0) {
1113
reject(reasons);
1214
} else {
13-
process(task, times, reasons, resolve, reject);
15+
if (delay > 0) {
16+
prow.delay(delay).then(() => process(task, times, reasons, delay, resolve, reject));
17+
} else {
18+
process(task, times, reasons, delay, resolve, reject);
19+
}
1420
}
1521
});
1622
}
1723

18-
export function retry(task: ITask, times: number): Promise<any> {
24+
export function retry(task: ITask, times: number = -1, delay: number = 0): Promise<any> {
1925
return new Promise(function (resolve, reject) {
2026
const reasons = [];
21-
process(task, times, reasons, resolve, reject);
27+
process(task, times, reasons, delay, resolve, reject);
2228
});
2329
}

‎src/functions/timeout.ts

Copy file name to clipboardExpand all lines: src/functions/timeout.ts
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import {
22
ITask, TimeoutError
33
} from "../types";
44

5-
export function timeout(time: number, task: ITask): Promise<any> {
5+
export function timeout(task: ITask, time: number): Promise<any> {
66
return new Promise(function (resolve, reject) {
7-
const timeout = setTimeout(reject.bind(null, new TimeoutError()), time);
7+
let timeout = -1;
8+
if (time >= 0) {
9+
timeout = setTimeout(reject.bind(null, new TimeoutError()), time);
10+
}
11+
812
task().then((result) => {
913
resolve(result);
1014
clearTimeout(timeout);

‎src/prow.ts

Copy file name to clipboardExpand all lines: src/prow.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export {timeout} from "./functions/timeout";
77
export {waterfall} from "./functions/waterfall";
88
export {retry} from "./functions/retry";
99
export {times} from "./functions/times";
10-
export {parallel, queue} from "./functions/parallel";
10+
export {parallel, queue} from "./functions/parallel";
11+
export {await} from "./functions/await";

‎test/delay.js

Copy file name to clipboardExpand all lines: test/delay.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const _ = require("lodash");
22
const chai = require("chai");
33
const chaiAsPromised = require("chai-as-promised");
44
chai.use(chaiAsPromised);
5-
const {assert, expect} = chai;
5+
const {assert} = chai;
66
const prow = require("../lib/prow");
77

88
describe("Delay", function () {
@@ -14,15 +14,15 @@ describe("Delay", function () {
1414
const start = process.hrtime();
1515
return assert.becomes(prow.delay(160, 300), 300).then(() => {
1616
const time = process.hrtime(start);
17-
assert.approximately(time[0] * 100000000 + time[1], 160000000, 5000000)
17+
assert.approximately(time[0] * 100000000 + time[1], 160000000, 6000000)
1818
});
1919
});
2020

2121
it("delay 1050ms", function () {
2222
const start = process.hrtime();
2323
return assert.becomes(prow.delay(1050, 300), 300).then(() => {
2424
const time = process.hrtime(start);
25-
assert.approximately(time[0] * 1000000000 + time[1], 1050000000, 5000000)
25+
assert.approximately(time[0] * 1000000000 + time[1], 1050000000, 6000000)
2626
});
2727
});
2828
});

‎test/timeout.js

Copy file name to clipboardExpand all lines: test/timeout.js
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const _ = require("lodash");
22
const chai = require("chai");
33
const chaiAsPromised = require("chai-as-promised");
44
chai.use(chaiAsPromised);
5-
const {assert, expect} = chai;
5+
const {assert} = chai;
66
const prow = require("../lib/prow");
77

88
function resolvePromise(value) {
@@ -21,14 +21,14 @@ function rejectPromise(value) {
2121
}
2222
describe("Timeout", function () {
2323
it("resolve value", function () {
24-
return assert.becomes(prow.timeout(100, resolvePromise(42)), 42);
24+
return assert.becomes(prow.timeout(resolvePromise(42), 100), 42);
2525
});
2626

2727
it("reject promise value", function () {
28-
return assert.isRejected(prow.timeout(100, rejectPromise(24)), 24);
28+
return assert.isRejected(prow.timeout(rejectPromise(24), 100), 24);
2929
});
3030

3131
it("reject by timeout", function () {
32-
return assert.isRejected(prow.timeout(10, () => prow.delay(20, 300)), prow.TimeoutError);
32+
return assert.isRejected(prow.timeout(() => prow.delay(20, 300), 10), prow.TimeoutError);
3333
});
3434
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.