39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
|
/** @license MIT License (c) copyright 2010-2014 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
(function(define) { 'use strict';
|
||
|
define(function() {
|
||
|
|
||
|
return function addWith(Promise) {
|
||
|
/**
|
||
|
* Returns a promise whose handlers will be called with `this` set to
|
||
|
* the supplied receiver. Subsequent promises derived from the
|
||
|
* returned promise will also have their handlers called with receiver
|
||
|
* as `this`. Calling `with` with undefined or no arguments will return
|
||
|
* a promise whose handlers will again be called in the usual Promises/A+
|
||
|
* way (no `this`) thus safely undoing any previous `with` in the
|
||
|
* promise chain.
|
||
|
*
|
||
|
* WARNING: Promises returned from `with`/`withThis` are NOT Promises/A+
|
||
|
* compliant, specifically violating 2.2.5 (http://promisesaplus.com/#point-41)
|
||
|
*
|
||
|
* @param {object} receiver `this` value for all handlers attached to
|
||
|
* the returned promise.
|
||
|
* @returns {Promise}
|
||
|
*/
|
||
|
Promise.prototype['with'] = Promise.prototype.withThis = function(receiver) {
|
||
|
var p = this._beget();
|
||
|
var child = p._handler;
|
||
|
child.receiver = receiver;
|
||
|
this._handler.chain(child, receiver);
|
||
|
return p;
|
||
|
};
|
||
|
|
||
|
return Promise;
|
||
|
};
|
||
|
|
||
|
});
|
||
|
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
|
||
|
|