Commit 793799b4 authored by analog-nico's avatar analog-nico
Browse files

Exposing Bluebird methods with validating name collisions

parent 907a75dd
Loading
Loading
Loading
Loading
+26 −15
Original line number Original line Diff line number Diff line
@@ -2,7 +2,8 @@


var Bluebird = require('./bluebird-fresh.js'),
var Bluebird = require('./bluebird-fresh.js'),
    printRejectionReason = require('./bluebird-captured-trace-fresh.js').possiblyUnhandledRejection,
    printRejectionReason = require('./bluebird-captured-trace-fresh.js').possiblyUnhandledRejection,
    _ = require('lodash');
    _ = require('lodash'),
    chalk = require('chalk');




// Load Request freshly - so that users can require an unaltered request instance!
// Load Request freshly - so that users can require an unaltered request instance!
@@ -118,24 +119,34 @@ function markPromiseInUse(requestInstance) {
    requestInstance._rp_promise._rp_in_use = true;
    requestInstance._rp_promise._rp_in_use = true;
}
}


request.Request.prototype.then = function RP$then(onFulfilled, onRejected) {
function expose(methodToExpose, exposeAs) {
    markPromiseInUse(this);
    return this._rp_promise.then.apply(this._rp_promise, arguments);
};


request.Request.prototype.catch = function RP$catch(onRejected) {
    exposeAs = exposeAs || methodToExpose;
    markPromiseInUse(this);
    return this._rp_promise.catch.apply(this._rp_promise, arguments);
};


request.Request.prototype.finally = function RP$finally() {
    /* istanbul ignore if */
    if (!_.isUndefined(request.Request.prototype[exposeAs])) {
        console.error(chalk.bold.bgRed('[Request-Promise] Unable to expose method "' + exposeAs + '". It is already implemented by Request.'));
        return;
    }

    request.Request.prototype[exposeAs] = function RP$exposed() {
        markPromiseInUse(this);
        markPromiseInUse(this);
    return this._rp_promise.finally.apply(this._rp_promise);
        return this._rp_promise[methodToExpose].apply(this._rp_promise, arguments);
    };
    };


request.Request.prototype.caught = request.Request.prototype.catch;
}


request.Request.prototype.lastly = request.Request.prototype.finally;
_.forEach([
        ['then'],
        ['catch'],
        ['caught'],
        ['finally'],
        ['lastly']
    ],
    function (args) {
        expose.apply(undefined, args);
    }
);


request.Request.prototype.promise = function RP$promise() {
request.Request.prototype.promise = function RP$promise() {
    markPromiseInUse(this);
    markPromiseInUse(this);