Skip to content

Commit

Permalink
use native Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey12313 committed Nov 16, 2017
1 parent dddca25 commit 4c36870
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage
html-report
html-report
package-lock.json
36 changes: 19 additions & 17 deletions lib/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter;
var state = require('./state');
var Q = require('q');
var commands = require('./command');

//base context
Expand Down Expand Up @@ -110,30 +109,33 @@ Context.prototype.send = function (msg, cb) {

Context.prototype.end = function () {
this.stream.end();
return Q.resolve();
return Promise.resolve();
};

Context.prototype.sendCommand = function (command) {
var defer = new Q.defer();
if (this.debug) console.log('command', command);
var self = this;
this.send(command + '\n', function (err, result) {
if (self.debug) console.log('err:', err, 'result:', result);
if (err) {
defer.reject(err);
} else {
defer.resolve(result);
}
});
return defer.promise;
return new Promise(function (resolve, reject) {
self.send(command + '\n', function (err, result) {
if (self.debug) console.log('err:', err, 'result:', result);
if (err) {
reject(err);
} else {
resolve(result);
}
});
})


};

Context.prototype.onEvent = function (event) {
var defer = new Q.defer();
this.on(event, function (data) {
defer.resolve(data);
});
return defer.promise;
var self = this
return new Promise(function (resolve) {
self.on(event, function (data) {
resolve(data);
})
})
};

//additional agi commands
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"node": ">=0.8.0"
},
"dependencies": {
"q": "^1.4.1",
"readable-stream": "*"
},
"devDependencies": {
Expand Down
8 changes: 3 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ describe('Context', function() {
context.exec('test')
.then(function (res) {
expect(res.result).to.eql('0');
process.nextTick(function() {
context.stream.write('200 result=1\n');
});
return context.exec('test 2');
})
.then(function (res) {
expect(res.result).to.eql('1');
done();
});

process.nextTick(function() {
context.stream.write('200 result=1\n');
});

});
});
});
Expand Down

0 comments on commit 4c36870

Please sign in to comment.