Commit 4c368700 authored by Sergey Razuvaev's avatar Sergey Razuvaev

use native Promise

parent dddca25e
node_modules/ node_modules/
coverage coverage
html-report html-report
\ No newline at end of file package-lock.json
var Readable = require('readable-stream'); var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var state = require('./state'); var state = require('./state');
var Q = require('q');
var commands = require('./command'); var commands = require('./command');
//base context //base context
...@@ -110,30 +109,33 @@ Context.prototype.send = function (msg, cb) { ...@@ -110,30 +109,33 @@ Context.prototype.send = function (msg, cb) {
Context.prototype.end = function () { Context.prototype.end = function () {
this.stream.end(); this.stream.end();
return Q.resolve(); return Promise.resolve();
}; };
Context.prototype.sendCommand = function (command) { Context.prototype.sendCommand = function (command) {
var defer = new Q.defer();
if (this.debug) console.log('command', command); if (this.debug) console.log('command', command);
var self = this; var self = this;
this.send(command + '\n', function (err, result) { return new Promise(function (resolve, reject) {
if (self.debug) console.log('err:', err, 'result:', result); self.send(command + '\n', function (err, result) {
if (err) { if (self.debug) console.log('err:', err, 'result:', result);
defer.reject(err); if (err) {
} else { reject(err);
defer.resolve(result); } else {
} resolve(result);
}); }
return defer.promise; });
})
}; };
Context.prototype.onEvent = function (event) { Context.prototype.onEvent = function (event) {
var defer = new Q.defer(); var self = this
this.on(event, function (data) { return new Promise(function (resolve) {
defer.resolve(data); self.on(event, function (data) {
}); resolve(data);
return defer.promise; })
})
}; };
//additional agi commands //additional agi commands
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
"node": ">=0.8.0" "node": ">=0.8.0"
}, },
"dependencies": { "dependencies": {
"q": "^1.4.1",
"readable-stream": "*" "readable-stream": "*"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -135,17 +135,15 @@ describe('Context', function() { ...@@ -135,17 +135,15 @@ describe('Context', function() {
context.exec('test') context.exec('test')
.then(function (res) { .then(function (res) {
expect(res.result).to.eql('0'); expect(res.result).to.eql('0');
process.nextTick(function() {
context.stream.write('200 result=1\n');
});
return context.exec('test 2'); return context.exec('test 2');
}) })
.then(function (res) { .then(function (res) {
expect(res.result).to.eql('1'); expect(res.result).to.eql('1');
done(); done();
}); });
process.nextTick(function() {
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