-
Notifications
You must be signed in to change notification settings - Fork 4
/
some.js
51 lines (43 loc) · 942 Bytes
/
some.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const arrayToObj = require('./lib/arrayToObj');
const objToArray = require('./lib/objToArray');
function onValue (value) {
return {
error: null,
value: value
};
}
function onError (error) {
return {
error: error,
value: null
};
}
function extractValue (result) {
return result.value || null;
}
function extractError (result) {
return result.error || null;
}
function onThenWithErrorsFactory (keys) {
return function onThenWithErrors (results) {
return [
arrayToObj(keys, results.map(extractError)),
arrayToObj(keys, results.map(extractValue))
];
};
}
function some (iterable) {
const [keys, items] = objToArray(iterable);
const promises = [];
for (let value of items) {
promises.push(Promise
.resolve(value)
.then(onValue, onError)
);
}
return Promise
.all(promises)
.then(onThenWithErrorsFactory(keys));
}
module.exports = some;