Skip to content

Commit ed9f6f5

Browse files
committed
wip
1 parent dffe908 commit ed9f6f5

File tree

9 files changed

+109
-16
lines changed

9 files changed

+109
-16
lines changed

src/functions/delay.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function delay(time: number, value?: any): Promise<any> {
2+
return new Promise(function (resolve) {
3+
setTimeout(resolve.bind(null, value), time);
4+
});
5+
}

src/functions/parallel.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {
2+
Tasks
3+
} from "../types";
4+
5+
6+
export function parallel(tasks: Tasks, maxThreads?: number): Promise<any> {
7+
if (!maxThreads) {
8+
maxThreads = tasks.length;
9+
}
10+
11+
return new Promise(function (resolve, reject) {
12+
13+
14+
});
15+
}

src/functions/retry.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
Task
3+
} from "../types";
4+
5+
function process(task: Task, times: number, reasons: any[], resolve, reject) {
6+
task().then((result) => {
7+
resolve(result);
8+
}).catch((reason) => {
9+
reasons.push(reason);
10+
if (reasons.length >= times) {
11+
reject(reasons);
12+
} else {
13+
process(task, times, resolve, reject, reasons);
14+
}
15+
});
16+
}
17+
18+
export function retry(task: Task, times: number): Promise<any> {
19+
return new Promise(function (resolve, reject) {
20+
const reasons = [];
21+
process(task, times, reasons, resolve, reject);
22+
});
23+
}

src/functions/timeout.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
Task, TimeoutError
3+
} from "../types";
4+
5+
export function timeout(time: number, task: Task): Promise<any> {
6+
return new Promise(function (resolve, reject) {
7+
const timeout = setTimeout(reject.bind(null, new TimeoutError()), time);
8+
task().then((result) => {
9+
resolve(result);
10+
clearTimeout(timeout);
11+
}, (reason) => {
12+
reject(reason);
13+
clearTimeout(timeout);
14+
});
15+
});
16+
}

src/functions/times.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
Task
3+
} from "../types";
4+
5+
6+
export function times(task: Task, times: number, stopOnFirstReject: boolean): Promise<any> {
7+
return new Promise(function (resolve, reject) {
8+
const results = [];
9+
10+
});
11+
}

src/functions/waterfall.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
Tasks
3+
} from "../types";
4+
5+
export function waterfall(tasks: Tasks): Promise<any> {
6+
if (tasks.length === 0) {
7+
return Promise.resolve();
8+
}
9+
10+
let promise = tasks[0]();
11+
if (tasks.length > 1) {
12+
for (const task of tasks) {
13+
promise = promise.then(task);
14+
}
15+
}
16+
return promise;
17+
}

src/prow.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import {
2-
Task, Tasks
1+
export {
2+
Task, Tasks, TimeoutError
33
} from "./types";
44

5-
export function waterfall(tasks: Tasks): Promise<any> {
6-
if (tasks.length === 0) {
7-
return Promise.resolve();
8-
}
9-
10-
let promise = tasks[0]();
11-
if (tasks.length > 1) {
12-
for (const task of tasks) {
13-
promise = promise.then(task);
14-
}
15-
}
16-
return promise;
17-
}
5+
export {delay} from "./functions/delay";
6+
export {timeout} from "./functions/timeout";
7+
export {waterfall} from "./functions/waterfall";
8+
export {retry} from "./functions/retry";
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface Task {
22
(...args: any[]): Promise<any>
33
}
4-
export type Tasks = Task[];
4+
export type Tasks = Task[];
5+
6+
export class TimeoutError extends Error{
7+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"module": "commonjs",
99
"removeComments": false,
1010
"lib": [
11+
"dom",
1112
"es5",
1213
"es2015.promise"
1314
]

0 commit comments

Comments
 (0)