forked from HowProgrammingWorks/AsynchronousProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha-to-async.js
More file actions
39 lines (30 loc) · 685 Bytes
/
a-to-async.js
File metadata and controls
39 lines (30 loc) · 685 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
36
37
38
39
'use strict';
// Sync function to async
const last = (arr) => arr[arr.length - 1];
const asyncify = (fn) => (...args) => {
const callback = last(args);
args.pop();
process.nextTick(() => {
callback(null, fn(...args));
});
};
// Functions
const f1 = (par) => par;
const f2 = (par) => par;
const f3 = (par) => par;
const f4 = (par) => par;
// Usage
const af1 = asyncify(f1);
const af2 = asyncify(f2);
const af3 = asyncify(f3);
const af4 = asyncify(f4);
af1('value2', (e, data) => {
af2(data, (e, data) => {
af3(data, (e, data) => {
af4(data, (e, data) => {
console.dir({ data });
});
});
});
});
console.log(f4(f3(f2(f1('value')))));