From 4c368700662a99baeefce6085b0dd3bc996d4487 Mon Sep 17 00:00:00 2001 From: Sergey Razuvaev Date: Thu, 16 Nov 2017 21:33:13 +0300 Subject: [PATCH] use native Promise --- .gitignore | 3 ++- lib/context.js | 36 +++++++++++++++++++----------------- package.json | 1 - test/index.js | 8 +++----- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 1b6cf6b..b2de383 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ coverage -html-report \ No newline at end of file +html-report +package-lock.json diff --git a/lib/context.js b/lib/context.js index 6466647..9f3c14d 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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 @@ -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 diff --git a/package.json b/package.json index e644007..61bafab 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "node": ">=0.8.0" }, "dependencies": { - "q": "^1.4.1", "readable-stream": "*" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index ebead31..559cc57 100644 --- a/test/index.js +++ b/test/index.js @@ -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'); - }); - }); }); });