lib/disguise.js
function disguiseThenable(target, source) {
  if (target === source) return target

  if (target.then instanceof Function) {
    var target_then = target.then

    function then(onFulfilled, onRejected) {
      if (onFulfilled != null) onFulfilled = onFulfilled.bind(target)
      if (onRejected != null) onRejected = onRejected.bind(target)

      var promise = target_then.call(target, onFulfilled, onRejected)

      return disguiseThenable(promise, source)
    }

    Object.defineProperties(target, {
      then: {
        value: then
      },
      catch: {
        value: promiseCatch
      }
    })
  }

  return disguise(target, source)
}
function disguiseThenable(target, source) {
  if (target === source) return target

  if (target.then instanceof Function) {
    var target_then = target.then

    function then(onFulfilled, onRejected) {
      if (onFulfilled != null) onFulfilled = onFulfilled.bind(target)
      if (onRejected != null) onRejected = onRejected.bind(target)

      var promise = target_then.call(target, onFulfilled, onRejected)

      return disguiseThenable(promise, source)
    }

    Object.defineProperties(target, {
      then: {
        value: then
      },
      catch: {
        value: promiseCatch
      }
    })
  }

  return disguise(target, source)
}
function disguiseThenable ( target , source ) { if ( target === source ) return target if ( target . then instanceof Function ) { var target_then = target . then function then ( onFulfilled , onRejected ) { if ( onFulfilled != null ) onFulfilled = onFulfilled . bind ( target ) if ( onRejected != null ) onRejected = onRejected . bind ( target ) var promise = target_then . call ( target , onFulfilled , onRejected ) return disguiseThenable ( promise , source ) } Object . defineProperties ( target , { then : { value : then } , catch : { value : promiseCatch } } ) } return disguise ( target , source ) }
Disguise a thenable object

If available, `target.then()` gets replaced by a method that exec the
`onFulfilled` and `onRejected` callbacks using `source` as `this` object, and
return the Promise returned by the original `target.then()` method already
disguised. It also add a `target.catch()` method pointing to the newly added
`target.then()`, being it previously available or not.

@param {thenable} target - the object to be disguised
@param {Object} source - the object where to fetch its methods and attributes

@return {thenable} `target` disguised
Disguise a thenable object