@trenskow/parallel 0.0.1 → 0.0.2
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 +52 -10
- package/package.json +9 -2
- package/test.js +44 -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,54 @@
|
|
|
1
|
-
// Created 2022
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// Created 2022 by Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
export default (promises) => {
|
|
4
|
+
|
|
5
|
+
if (!Array.isArray(promises)) promises = [promises];
|
|
6
|
+
|
|
7
|
+
promises.forEach((promise) => {
|
|
8
|
+
if (typeof (promise || {}).then !== 'function') throw new Error('Promise is not thenable.');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
let results = Array(promises.length).fill({});
|
|
12
|
+
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
|
|
15
|
+
const evaluate = () => {
|
|
16
|
+
|
|
17
|
+
if (!results) return;
|
|
18
|
+
|
|
19
|
+
if (results.filter(({ status }) => status).length === promises.length) {
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
resolve(results.map((result) => {
|
|
23
|
+
if (result.status === 'rejected') throw result.reason;
|
|
24
|
+
return result.value;
|
|
25
|
+
}));
|
|
26
|
+
} catch (error) {
|
|
27
|
+
reject(error);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
results = undefined;
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
promises.forEach((promise, idx) => {
|
|
37
|
+
promise
|
|
38
|
+
.then((value) => {
|
|
39
|
+
results[idx] = {
|
|
40
|
+
status: 'resolved',
|
|
41
|
+
value
|
|
42
|
+
};
|
|
43
|
+
}, (reason) => {
|
|
44
|
+
results[idx] = {
|
|
45
|
+
status: 'rejected',
|
|
46
|
+
reason
|
|
47
|
+
};
|
|
48
|
+
})
|
|
49
|
+
.then(evaluate);
|
|
9
50
|
});
|
|
10
|
-
};
|
|
11
51
|
|
|
12
|
-
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/parallel",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
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,44 @@
|
|
|
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 eventually come back with [1, 2, 3]', async () => {
|
|
20
|
+
const called = [false, false, false];
|
|
21
|
+
return (async () => {
|
|
22
|
+
await expect(parallel([0, 1, 2].map(async (item) => {
|
|
23
|
+
await wait(`${item}s`);
|
|
24
|
+
called[item] = true;
|
|
25
|
+
return item + 1;
|
|
26
|
+
}))).to.eventually.be.eql([1, 2, 3]);
|
|
27
|
+
expect(called).to.eql([true, true, true]);
|
|
28
|
+
})();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it ('should eventually be rejected', async () => {
|
|
32
|
+
const called = [false, false, false];
|
|
33
|
+
return (async () => {
|
|
34
|
+
await expect(parallel([0, 1, 2].map(async (item) => {
|
|
35
|
+
if (item === 1) throw new Error('Some error');
|
|
36
|
+
await wait(`${item}s`);
|
|
37
|
+
called[item] = true;
|
|
38
|
+
return item + 1;
|
|
39
|
+
}))).to.eventually.be.rejectedWith('Some error');
|
|
40
|
+
expect(called).to.eql([true, false, true]);
|
|
41
|
+
})();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
});
|