Commit f52fa9c8 authored by antirek's avatar antirek

promisify context.exec

parent dc1f649a
...@@ -95,16 +95,6 @@ Context.prototype.end = function () { ...@@ -95,16 +95,6 @@ Context.prototype.end = function () {
return Q.resolve(); return Q.resolve();
}; };
Context.prototype.exec = function() {
var args = Array.prototype.slice.call(arguments, 0);
var last = args.pop();
if(typeof last !== 'function') {
args.push(last);
last = function () { }
}
this.send('EXEC ' + args.join(' ') + '\n', last);
};
Context.prototype.sendCommand = function (command) { Context.prototype.sendCommand = function (command) {
var defer = new Q.defer(); var defer = new Q.defer();
this.send(command + '\n', function (err, result){ this.send(command + '\n', function (err, result){
...@@ -123,10 +113,15 @@ Context.prototype.onEvent = function (event) { ...@@ -123,10 +113,15 @@ Context.prototype.onEvent = function (event) {
defer.resolve(data); defer.resolve(data);
}); });
return defer.promise; return defer.promise;
} };
Context.prototype.exec = function() {
var args = Array.prototype.slice.call(arguments, 0);
return this.sendCommand('EXEC ' + args.join(' '));
};
Context.prototype.dial = function (num, timeout, params, cb) { Context.prototype.dial = function (num, timeout, params) {
this.exec('Dial', num + ',' + timeout + ',' + params, cb); return this.exec('Dial', num + ',' + timeout + ',' + params);
}; };
Context.prototype.databaseDel = function (family, key) { Context.prototype.databaseDel = function (family, key) {
......
...@@ -96,9 +96,10 @@ describe('Context', function() { ...@@ -96,9 +96,10 @@ describe('Context', function() {
context.stream.write('\n'); context.stream.write('\n');
}); });
context.exec('test', 'boom', function(err, res) { context.exec('test', 'boom').then(function(){
done(err); done();
}); });
}); });
}); });
...@@ -111,10 +112,12 @@ describe('Context', function() { ...@@ -111,10 +112,12 @@ describe('Context', function() {
context.stream.write('200 result=0\n'); context.stream.write('200 result=0\n');
}); });
context.exec('test', function(err, res) { context.exec('test')
.then(function (res) {
expect(res.result).to.eql('0'); expect(res.result).to.eql('0');
return context.exec('test 2');
context.exec('test 2', function(err, res) { })
.then(function (res) {
expect(res.result).to.eql('1'); expect(res.result).to.eql('1');
done(); done();
}); });
...@@ -122,7 +125,7 @@ describe('Context', function() { ...@@ -122,7 +125,7 @@ describe('Context', function() {
process.nextTick(function() { process.nextTick(function() {
context.stream.write('200 result=1\n'); context.stream.write('200 result=1\n');
}); });
});
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment