-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path7-functor.js
More file actions
51 lines (42 loc) · 1.02 KB
/
7-functor.js
File metadata and controls
51 lines (42 loc) · 1.02 KB
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 metasync = require('metasync');
const arrayChain = (array, prev = null) => {
let next = null,
done = null,
fail = null;
const self = (err, data) => {
array = data;
if (next) next();
if (err) {
if (fail) fail(err);
} else if (done) {
done(data);
}
};
if (!prev) process.nextTick(() => self(null, array));
self.then = (fn) => {
done = fn;
return self;
};
self.catch = (fn) => {
fail = fn;
return self;
};
self.fetch = (fn) =>
self.then((data) => fn(null, data)).catch((err) => fn(err));
const chain = (performer) => (fn, initial) => {
const res = arrayChain(null, self);
next = () => performer(array, fn, res, initial);
return res;
};
self.map = chain(metasync.map);
self.filter = chain(metasync.filter);
self.reduce = chain(metasync.reduce);
self.each = chain(metasync.each);
self.series = chain(metasync.series);
self.find = chain(metasync.find);
return self;
};
module.exports = {
for: arrayChain,
};