@trenskow/parallel 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{.eslintrc.js → .eslintrc.cjs} +0 -0
- package/.vscode/launch.json +18 -0
- package/index.js +55 -10
- package/package.json +9 -2
- package/test.js +52 -0
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "Test",
|
|
11
|
+
"skipFiles": [
|
|
12
|
+
"<node_internals>/**"
|
|
13
|
+
],
|
|
14
|
+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
|
|
15
|
+
"args": ["--bail","--timeout","0"]
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/index.js
CHANGED
|
@@ -1,12 +1,57 @@
|
|
|
1
|
-
// Created 2022
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// Created 2022 by Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
export default (promises) => {
|
|
4
|
+
|
|
5
|
+
if (typeof promises === 'undefined') throw new TypeError('No promises was provided.');
|
|
6
|
+
if (!Array.isArray(promises)) promises = [promises];
|
|
7
|
+
|
|
8
|
+
if (promises.length === 0) return Promise.resolve([]);
|
|
9
|
+
|
|
10
|
+
promises.forEach((promise) => {
|
|
11
|
+
if (typeof (promise || {}).then !== 'function') throw new Error('Promise is not thenable.');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
let results = Array(promises.length).fill({});
|
|
15
|
+
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
|
|
18
|
+
const evaluate = () => {
|
|
19
|
+
|
|
20
|
+
if (!results) return;
|
|
21
|
+
|
|
22
|
+
if (results.filter(({ status }) => status).length === promises.length) {
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
resolve(results.map((result) => {
|
|
26
|
+
if (result.status === 'rejected') throw result.reason;
|
|
27
|
+
return result.value;
|
|
28
|
+
}));
|
|
29
|
+
} catch (error) {
|
|
30
|
+
reject(error);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
results = undefined;
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
promises.forEach((promise, idx) => {
|
|
40
|
+
promise
|
|
41
|
+
.then((value) => {
|
|
42
|
+
results[idx] = {
|
|
43
|
+
status: 'resolved',
|
|
44
|
+
value
|
|
45
|
+
};
|
|
46
|
+
}, (reason) => {
|
|
47
|
+
results[idx] = {
|
|
48
|
+
status: 'rejected',
|
|
49
|
+
reason
|
|
50
|
+
};
|
|
51
|
+
})
|
|
52
|
+
.then(evaluate);
|
|
9
53
|
});
|
|
10
|
-
};
|
|
11
54
|
|
|
12
|
-
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/parallel",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "A small library for doing async tasks in parallel.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -22,5 +22,12 @@
|
|
|
22
22
|
"bugs": {
|
|
23
23
|
"url": "https://github.com/trenskow/parallel/issues"
|
|
24
24
|
},
|
|
25
|
-
"homepage": "https://github.com/trenskow/parallel#readme"
|
|
25
|
+
"homepage": "https://github.com/trenskow/parallel#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@trenskow/wait": "^1.2.2",
|
|
28
|
+
"chai": "^4.3.6",
|
|
29
|
+
"chai-as-promised": "^7.1.1",
|
|
30
|
+
"eslint": "^8.13.0",
|
|
31
|
+
"mocha": "^9.2.2"
|
|
32
|
+
}
|
|
26
33
|
}
|
package/test.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Created 2022 by Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
import chai from 'chai';
|
|
4
|
+
import chaiAsPromised from 'chai-as-promised';
|
|
5
|
+
import wait from '@trenskow/wait';
|
|
6
|
+
|
|
7
|
+
chai.use(chaiAsPromised);
|
|
8
|
+
|
|
9
|
+
const { expect } = chai;
|
|
10
|
+
|
|
11
|
+
import parallel from './index.js';
|
|
12
|
+
|
|
13
|
+
describe('parallel', () => {
|
|
14
|
+
|
|
15
|
+
it ('should throw an error if promises are not provided.', () => {
|
|
16
|
+
expect(() => parallel(123)).to.throw('Promise is not thenable.');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it ('should throw an error if no promises are provided.', () => {
|
|
20
|
+
expect(() => parallel()).to.throw('No promises was provided.');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it ('should come back with empty array if an empty array was provided.', async () => {
|
|
24
|
+
return expect(parallel([])).to.eventually.be.eql([]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it ('should eventually come back with [1, 2, 3]', async () => {
|
|
28
|
+
const called = [false, false, false];
|
|
29
|
+
return (async () => {
|
|
30
|
+
await expect(parallel([0, 1, 2].map(async (item) => {
|
|
31
|
+
await wait(`${item}s`);
|
|
32
|
+
called[item] = true;
|
|
33
|
+
return item + 1;
|
|
34
|
+
}))).to.eventually.be.eql([1, 2, 3]);
|
|
35
|
+
expect(called).to.eql([true, true, true]);
|
|
36
|
+
})();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it ('should eventually be rejected', async () => {
|
|
40
|
+
const called = [false, false, false];
|
|
41
|
+
return (async () => {
|
|
42
|
+
await expect(parallel([0, 1, 2].map(async (item) => {
|
|
43
|
+
if (item === 1) throw new Error('Some error');
|
|
44
|
+
await wait(`${item}s`);
|
|
45
|
+
called[item] = true;
|
|
46
|
+
return item + 1;
|
|
47
|
+
}))).to.eventually.be.rejectedWith('Some error');
|
|
48
|
+
expect(called).to.eql([true, false, true]);
|
|
49
|
+
})();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
});
|