-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathniceUtils.js
More file actions
49 lines (43 loc) · 1.48 KB
/
niceUtils.js
File metadata and controls
49 lines (43 loc) · 1.48 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
class NiceUtils {
// Consider this method to be the ultimate translator between
// promise-style and callback-style invocations.
// operation may accept a callback or return a promise.
// If done is not provided, this method returns a promise.
// resultLength is the number of parameters that we expect
// to pass to done.
performAsyncOperation(operation, resultLength, done) {
if (done) {
if (operation.length > 0) {
operation(done);
} else {
operation().then((result) => {
if (resultLength > 1) {
done(...result);
} else if (resultLength === 1) {
done(result);
} else {
done();
}
});
}
} else {
if (operation.length > 0) {
return new Promise((resolve) => {
operation((...args) => {
if (resultLength > 1) {
resolve(args);
} else if (resultLength === 1) {
resolve(args[0]);
} else {
resolve();
}
});
});
} else {
return operation();
}
}
}
}
const niceUtils = new NiceUtils();
module.exports = { niceUtils };