Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit fc7f55d

Browse files
committed
Remove promise plugin
1 parent 371e1d9 commit fc7f55d

File tree

1 file changed

+34
-78
lines changed

1 file changed

+34
-78
lines changed

lib/client.js

Lines changed: 34 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const pkg = require('../package.json');
2323
const utils = require('./clientutils.js');
2424

2525
const DEFAULTS = {
26-
maxAttempt: 3,
27-
usePromises: false
26+
maxAttempt: 3
2827
};
2928

3029
/**
@@ -42,7 +41,6 @@ class CloudantClient {
4241
var client;
4342
self._plugins = [];
4443
self._pluginIds = [];
45-
self._usePromises = false;
4644
self.useLegacyPlugin = false;
4745

4846
// Build plugin array.
@@ -120,12 +118,6 @@ class CloudantClient {
120118
if (plugin === 'base' || plugin === 'default') {
121119
return; // noop
122120
}
123-
if (plugin === 'promises') {
124-
// maps this.request -> this.doPromisesRequest
125-
debug('Adding plugin: \'promises\'');
126-
self._cfg.usePromises = true;
127-
return;
128-
}
129121

130122
try {
131123
Plugin = require('../plugins/' + plugin);
@@ -196,35 +188,43 @@ class CloudantClient {
196188
this._client = require('request').defaults(requestDefaults);
197189
}
198190

199-
_doPromisesRequest(options, callback) {
200-
var self = this;
191+
_executeRequest(request, done) {
192+
debug('Submitting request: %j', request.options);
201193

202-
return new Promise(function(resolve, reject) {
203-
self._doRequest(options, function(error, response, data) {
204-
if (typeof callback !== 'undefined') {
205-
callback(error, response, data);
206-
}
207-
if (error) {
208-
reject(error);
209-
} else {
210-
if (data) {
211-
try {
212-
data = JSON.parse(data);
213-
} catch (err) {}
214-
} else {
215-
data = { statusCode: response.statusCode };
216-
}
217-
if (response.statusCode >= 200 && response.statusCode < 400) {
218-
resolve(data);
219-
} else {
220-
reject(new CloudantError(response, data));
221-
}
222-
}
194+
request.response = this._client(
195+
request.options, utils.wrapCallback(request, done));
196+
197+
// define new source on event relay
198+
request.eventRelay.setSource(request.response);
199+
200+
request.response
201+
.on('response', function(response) {
202+
request.response.pause();
203+
utils.runHooks('onResponse', request, response, function() {
204+
utils.processState(request, done); // process response hook results
205+
});
223206
});
224-
});
207+
208+
if (typeof request.clientCallback === 'undefined') {
209+
debug('No client callback specified.');
210+
request.response
211+
.on('error', function(error) {
212+
utils.runHooks('onError', request, error, function() {
213+
utils.processState(request, done); // process error hook results
214+
});
215+
});
216+
}
225217
}
226218

227-
_doRequest(options, callback) {
219+
// public
220+
221+
/**
222+
* Perform a request using this Cloudant client.
223+
*
224+
* @param {Object} options - HTTP options.
225+
* @param {requestCallback} callback - The callback that handles the response.
226+
*/
227+
request(options, callback) {
228228
var self = this;
229229

230230
if (typeof options === 'string') {
@@ -346,50 +346,6 @@ class CloudantClient {
346346

347347
return request.clientStream; // return stream to client
348348
}
349-
350-
_executeRequest(request, done) {
351-
debug('Submitting request: %j', request.options);
352-
353-
request.response = this._client(
354-
request.options, utils.wrapCallback(request, done));
355-
356-
// define new source on event relay
357-
request.eventRelay.setSource(request.response);
358-
359-
request.response
360-
.on('response', function(response) {
361-
request.response.pause();
362-
utils.runHooks('onResponse', request, response, function() {
363-
utils.processState(request, done); // process response hook results
364-
});
365-
});
366-
367-
if (typeof request.clientCallback === 'undefined') {
368-
debug('No client callback specified.');
369-
request.response
370-
.on('error', function(error) {
371-
utils.runHooks('onError', request, error, function() {
372-
utils.processState(request, done); // process error hook results
373-
});
374-
});
375-
}
376-
}
377-
378-
// public
379-
380-
/**
381-
* Perform a request using this Cloudant client.
382-
*
383-
* @param {Object} options - HTTP options.
384-
* @param {requestCallback} callback - The callback that handles the response.
385-
*/
386-
request(options, callback) {
387-
if (this._cfg.usePromises) {
388-
return this._doPromisesRequest(options, callback);
389-
} else {
390-
return this._doRequest(options, callback);
391-
}
392-
}
393349
}
394350

395351
module.exports = CloudantClient;

0 commit comments

Comments
 (0)