Skip to content

Commit

Permalink
Make .then() return a real Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Mar 8, 2016
1 parent 47bb805 commit 43aaa4a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ exports.timeout = function timeout(ms){
};

/**
* Faux promise support
* Promise support
*
* @param {Function} fulfill
* @param {Function} resolve
* @param {Function} reject
* @return {Request}
*/

exports.then = function then(fulfill, reject) {
return this.end(function(err, res) {
err ? reject(err) : fulfill(res);
});
exports.then = function then(resolve, reject) {
if (!this._fullfilledPromise) {
var self = this;
this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
self.end(function(err, res){
if (err) innerReject(err); else innerResolve(res);
});
});
}
return this._fullfilledPromise.then(resolve, reject);
}

/**
Expand Down
52 changes: 52 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ describe('request', function(){
done();
});
})

it('is optional with a promise', function() {
if ('undefined' === typeof Promise) {
return;
}

return request.get(uri + '/login')
.then(function(res) {
return res.status;
})
.then()
.then(function(status) {
assert.equal(200, status, "Real promises pass results through");
});
});

it('called only once with a promise', function() {
if ('undefined' === typeof Promise) {
return;
}

var req = request.get(uri + '/unique');

return Promise.all([req, req, req])
.then(function(results){
results.forEach(function(item){
assert.equal(item.body, results[0].body, "It should keep returning the same result after being called once");
});
});
});
})

describe('res.error', function(){
Expand All @@ -83,6 +113,20 @@ describe('request', function(){
done();
});
})

it('with .then() promise', function(){
if ('undefined' === typeof Promise) {
return;
}

return request
.get(uri + '/error')
.then(function(){
assert.fail();
}, function(err){
assert.equal(err.message, 'Internal Server Error');
});
})
})

describe('res.header', function(){
Expand Down Expand Up @@ -312,6 +356,10 @@ describe('request', function(){

describe('.then(fulfill, reject)', function() {
it('should support successful fulfills with .then(fulfill)', function(done) {
if ('undefined' === typeof Promise) {
return done();
}

request
.post(uri + '/echo')
.send({ name: 'tobi' })
Expand All @@ -322,6 +370,10 @@ describe('request', function(){
})

it('should reject an error with .then(null, reject)', function(done) {
if ('undefined' === typeof Promise) {
return done();
}

request
.get(uri + '/error')
.then(null, function(err) {
Expand Down
5 changes: 5 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ app.all('/echo', function(req, res){
req.pipe(res);
});

var uniq = 0;
app.all('/unique', function(req, res){
res.send('never the same ' + (uniq++));
});

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
Expand Down

0 comments on commit 43aaa4a

Please sign in to comment.