forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
49 lines (44 loc) · 1.01 KB
/
utils.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
'use strict';
const errorObject = { value: null };
function tryCatch(fn, ctx, args) {
try {
return fn.apply(ctx, args);
} catch (e) {
errorObject.value = e;
return errorObject;
}
}
function isEmpty(obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
/**
* Waits for a redis client to be ready.
* @param {Redis} redis client
*/
function isRedisReady(client) {
return new Promise((resolve, reject) => {
if (client.status === 'ready') {
resolve();
} else {
function handleReady() {
client.removeListener('error', handleError);
resolve();
}
function handleError(err) {
client.removeListener('ready', handleReady);
reject(err);
}
client.once('ready', handleReady);
client.once('error', handleError);
}
});
}
module.exports.errorObject = errorObject;
module.exports.tryCatch = tryCatch;
module.exports.isEmpty = isEmpty;
module.exports.isRedisReady = isRedisReady;