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 dffe908

Browse filesBrowse files
committed
waterfall
1 parent 3b51d32 commit dffe908
Copy full SHA for dffe908

File tree

6 files changed

+157
-7
lines changed
Filter options

6 files changed

+157
-7
lines changed

‎README.md

Copy file name to clipboard
+27-4Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
# Steersman
1+
# JS Promises Flow Control
22

3-
_Lightweight JS router for browsers & node_
4-
5-
Work in progress
3+
## Install
4+
```
5+
npm install prow
6+
```
7+
8+
## API
9+
10+
### prow.when(deferreds)
11+
### prow.nextTick(task)
12+
### prow.defer(timeout, timelimit)
13+
### prow.waterfall(tasks)
14+
### prow.parallel(tasks, maxThreads, managed)
15+
### prow.queue(tasks, managed)
16+
### prow.retry(task, times, delay)
17+
### prow.times(task, times)
18+
### prow.await(condition, checkDelay, timeLimit)
19+
20+
## License
21+
22+
Copyright (c) 2015 rzcoder
23+
24+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
25+
26+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎package.json

Copy file name to clipboardExpand all lines: package.json
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
"scripts": {
2222
"build": "tsc",
2323
"test": "mocha",
24-
"-pretest": "tslint --project ./"
24+
"lint": "tslint --project ./"
2525
},
2626
"devDependencies": {
2727
"chai": "^3.5.0",
2828
"chai-as-promised": "^6.0.0",
29-
"mocha": "^3.1.2"
29+
"lodash": "^4.16.4",
30+
"mocha": "^3.1.2",
31+
"tslint": "^3.15.1",
32+
"typescript": "^2.0.6"
3033
}
3134
}

‎src/prow.ts

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
Task, 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/types.ts

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Task {
2+
(...args: any[]): Promise<any>
3+
}
4+
export type Tasks = Task[];

‎test/waterfall.js

Copy file name to clipboard
+104Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const _ = require("lodash");
2+
const chai = require("chai");
3+
const chaiAsPromised = require("chai-as-promised");
4+
chai.use(chaiAsPromised);
5+
const {assert, expect} = chai;
6+
const prow = require("../lib/prow");
7+
8+
9+
function resolvePromise(value) {
10+
return function (data) {
11+
if (data) {
12+
return Promise.resolve(data + 1);
13+
}
14+
return Promise.resolve(value);
15+
}
16+
}
17+
18+
function rejectPromise(value) {
19+
return function () {
20+
return Promise.reject(value);
21+
}
22+
}
23+
24+
describe("Waterfall", function () {
25+
it("single resolved promise", function () {
26+
return assert.becomes(prow.waterfall([resolvePromise("resolved value")]), "resolved value");
27+
});
28+
29+
it("single rejected promise", function () {
30+
return assert.isRejected(prow.waterfall([rejectPromise("rejected reason")]), "rejected reason");
31+
});
32+
33+
it("few resolved promises", function () {
34+
return assert.becomes(prow.waterfall([resolvePromise(1), resolvePromise(), resolvePromise(), resolvePromise()]), 5);
35+
});
36+
37+
it("few resolved promises with rejected one", function () {
38+
return assert.isRejected(
39+
prow.waterfall([resolvePromise(), resolvePromise(), rejectPromise("rejected reason"), resolvePromise()]),
40+
"rejected reason"
41+
);
42+
});
43+
44+
it("data waterfall", function () {
45+
return assert.becomes(
46+
prow.waterfall([
47+
function () {
48+
return Promise.resolve("first");
49+
},
50+
function (data) {
51+
return Promise.resolve({
52+
[data]: 42
53+
});
54+
},
55+
function (data) {
56+
data["first"] *= 10;
57+
return Promise.resolve(data);
58+
},
59+
function (data) {
60+
return Promise.resolve(data).then(_.toPairs);
61+
},
62+
]),
63+
[["first", 420]]
64+
);
65+
});
66+
67+
it("combine waterfalls", function () {
68+
const promise = prow.waterfall([
69+
function () {
70+
return Promise.resolve("first");
71+
},
72+
function (data) {
73+
return [
74+
function () {
75+
return Promise.resolve({
76+
[data]: 42
77+
});
78+
},
79+
function (data) {
80+
return Promise.resolve(data.first + 10);
81+
}
82+
]
83+
}
84+
]).then(prow.waterfall).then((data) => {
85+
return prow.waterfall([
86+
function () {
87+
return Promise.resolve({
88+
[data]: "second"
89+
});
90+
},
91+
function (data) {
92+
data["third"] = data[52] + "_";
93+
return Promise.resolve(data);
94+
}
95+
]);
96+
});
97+
98+
return assert.becomes(promise, {
99+
"52": "second",
100+
"third": "second_"
101+
});
102+
});
103+
104+
});

‎tsconfig.json

Copy file name to clipboardExpand all lines: tsconfig.json
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"removeComments": false,
1010
"lib": [
1111
"es5",
12-
"dom",
1312
"es2015.promise"
1413
]
1514
},

0 commit comments

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