Skip to content

Commit 6b9fd01

Browse files
Add cancellation support (fixes #113)
1 parent 27945f3 commit 6b9fd01

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/rp.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var Bluebird = require('bluebird'),
3+
var Bluebird = require('bluebird').getNewLibraryCopy(),
44
configure = require('@request/promise-core/configure/request2'),
55
stealthyRequire = require('stealthy-require');
66

@@ -20,6 +20,7 @@ try {
2020
throw err;
2121
}
2222

23+
Bluebird.config({cancellation: true});
2324

2425
configure({
2526
request: request,
@@ -28,11 +29,17 @@ configure({
2829
'then',
2930
'catch',
3031
'finally',
32+
'cancel',
3133
'promise'
32-
]
34+
],
35+
constructorMixin: function (resolve, reject, onCancel) {
36+
var self = this;
37+
onCancel(function () {
38+
self.abort();
39+
});
40+
}
3341
});
3442

35-
3643
request.bindCLS = function RP$bindCLS() {
3744
throw new Error('CLS support was dropped. To get it back read: https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage');
3845
};

test/fixtures/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ module.exports = function startServer(port, cb) {
1919
res.writeHead(301, { Location: '/200' });
2020
res.end();
2121
break;
22+
case 503:
23+
// Send no response at all
24+
break;
2225
default:
2326
res.writeHead(status, {'Content-Type': 'text/plain'});
2427
var body = req.method === 'POST' ? ' - ' + JSON.stringify(req.body) : '';

test/spec/request-test.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var Bluebird = require('bluebird'),
4-
childProcess = require('child_process'),
3+
var childProcess = require('child_process'),
54
errors = require('../../errors'),
65
path = require('path'),
76
rp = require('../../'),
@@ -78,10 +77,27 @@ describe('Request-Promise', function () {
7877

7978
var p = rp('http://localhost:4000/200').promise();
8079

81-
expect(p instanceof Bluebird).to.eql(true);
80+
// This will not actually be an instanceof Bluebird since request-promise creates a new Bluebird copy.
81+
// Instead, verify that the Promise contains the bluebird functions.
82+
expect(p.constructor.name).to.equal('Promise');
83+
expect(p.then).to.be.a('function');
84+
expect(p.delay).to.be.a('function');
85+
expect(p.map).to.be.a('function');
86+
expect(p.cancel).to.be.a('function');
8287

8388
});
8489

90+
it('.cancel() cancelling the Bluebird promise and aborting the request', function (done) {
91+
var req = rp('http://localhost:4000/503');
92+
req.once('abort', done);
93+
req.cancel();
94+
});
95+
96+
it('.cancel() on promises chained from the Bluebird promise, aborting the request', function (done) {
97+
var req = rp('http://localhost:4000/503');
98+
req.once('abort', done);
99+
req.then(function noop() { }).cancel();
100+
});
85101
});
86102

87103
describe('should still allow to require Request independently', function () {

0 commit comments

Comments
 (0)