Skip to content

Commit cff3d10

Browse files
committed
[ENHANCEMENT] Return native Promise for methods if no callback is supplied.
1 parent 4519ac5 commit cff3d10

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/rpccall.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,38 @@ module.exports = (connection) => {
2222

2323
params = parseParams(commands[command], args);
2424

25-
} else if (args instanceof Function && !cb) {
26-
27-
cb = args;
28-
2925
} else if (Array.isArray(args) || args === null || args === undefined) {
3026

3127
params = args;
3228

33-
} else {
29+
} else if (args instanceof Function && !cb) {
30+
31+
cb = args;
32+
33+
} else if(args != undefined) {
3434

3535
throw `${args} is invalid input.`
3636

3737
}
3838

39-
client.call(command.toLowerCase(), params, (err, res) => {
40-
cb(err, res);
41-
})
39+
let promiseReturn = null;
40+
41+
if(!cb){
42+
promiseReturn = new Promise((resolve, reject) => {
43+
cb = (err, res) => {
44+
if(err){
45+
return reject(err);
46+
}
47+
resolve(res);
48+
}
49+
})
50+
}
51+
52+
53+
client.call(command.toLowerCase(), params, cb);
54+
55+
return promiseReturn;
56+
4257
}
4358
}
4459
return caller;

0 commit comments

Comments
 (0)