Commit a9fb8f01 authored by analog-nico's avatar analog-nico
Browse files

Allow calling then on a finished request

parent 1ce2cff8
Loading
Loading
Loading
Loading
+14 −23
Original line number Diff line number Diff line
@@ -16,10 +16,6 @@ function ownCallback(err, httpResponse, body) {
        } catch (e) { }
    }

    if (self._rp_then_invoked !== true) {
        return;
    }

    if (err) {
        self._rp_reject({
            error: err,
@@ -55,33 +51,28 @@ request.Request.prototype.init = function (options) {
        options.method = options.method.toUpperCase();
    }

    this._rp_callbackOrig = this.callback;
    this.callback = ownCallback;

    this._rp_options = options;
    this._rp_options.simple = options.simple === false ? false : true;
    this._rp_options.resolveWithFullResponse = options.resolveWithFullResponse === true ? true : false;

    return originalInit.apply(this, arguments);
    var self = this;

};
    self._rp_promise = new Bluebird(function (resolve, reject) {

request.Request.prototype.then = function (onFulfilled, onRejected) {
        self._rp_resolve = resolve;
        self._rp_reject = reject;

    var self = this;
    });

    if (self._rp_then_invoked === true) {
        throw new Error('Request-Promise currently only allows to call the rp(...).then(...) method once. Please use chaining like rp(...).then(...).then(...) instead.');
    }
    self._rp_callbackOrig = self.callback;
    self.callback = ownCallback;

    return new Bluebird(function (resolve, reject) {
    self._rp_options = options;
    self._rp_options.simple = options.simple === false ? false : true;
    self._rp_options.resolveWithFullResponse = options.resolveWithFullResponse === true ? true : false;

        self._rp_then_invoked = true;
        self._rp_resolve = resolve;
        self._rp_reject = reject;
    return originalInit.apply(self, arguments);

    }).then(onFulfilled, onRejected);
};

request.Request.prototype.then = function (onFulfilled, onRejected) {
    return this._rp_promise.then(onFulfilled, onRejected);
};

module.exports = request;
+27 −4
Original line number Diff line number Diff line
@@ -453,7 +453,17 @@ describe('Request-Promise', function () {

    describe('should be Promises/A+ compliant', function () {

        xit('and allow calling then after the request already finished');
        it('and allow calling then after the request already finished', function (done) {

            var req = rp('http://localhost:4000/200');

            setTimeout(function () {
                req.then(function () {
                    done();
                });
            }, 20);

        });

        it('and take only a fulfilled handler', function (done) {

@@ -506,10 +516,23 @@ describe('Request-Promise', function () {

        });

        it('but not allow the then method to be invoked more than once', function () {
        it('and allow the then method to be invoked more than once', function (done) {

            var countFulfillCalls = 0;

            function onFulfilled() {
                countFulfillCalls += 1;
                if (countFulfillCalls === 3) {
                    done();
                }
            }

            var req = rp('http://localhost:4000/200');
            req.then(null, function () { /* A sporadic ECONNREFUSED shall not fail the test. */ });
            expect(function () { req.then(); }).to.throw('Request-Promise currently only allows to call the rp(...).then(...) method once. Please use chaining like rp(...).then(...).then(...) instead.');

            req.then(onFulfilled);
            req.then(onFulfilled);
            req.then(onFulfilled);

        });

    });