-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstep.js
More file actions
35 lines (31 loc) · 734 Bytes
/
step.js
File metadata and controls
35 lines (31 loc) · 734 Bytes
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
const _ = require('lodash');
const dotty = require('dotty');
module.exports = (
stepFn
, fromCtx
, toCtx
) => {
return (ctx, cb) => {
cb = cb || _.noop;
var fromFn = fromCtx || _.identity;
var toFn = toCtx || ((c) => c);
if (_.isString(fromCtx)) {
fromFn = (c) => dotty.get(c, fromCtx);
}
if (_.isString(toCtx)) {
toFn = (c, r) => { dotty.put(c, toCtx, r); return c; };
}
return new Promise((resolve, reject) => {
try {
stepFn(_.cloneDeep(fromFn(ctx)), (err, res) => {
cb(err, toFn(ctx, res));
if (err) return reject(err);
resolve(toFn(ctx, res));
});
} catch (e) {
cb(e);
reject(e);
}
});
};
};