Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active January 2, 2016 04:18
Show Gist options
  • Save myndzi/8249218 to your computer and use it in GitHub Desktop.
Save myndzi/8249218 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
Promise.extend = function(method, fn) {
var Promise = this;
Promise.prototype[method] = function Promise$_Proxy() {
var $_len = arguments.length;
var args = new Array($_len + 1); for(var $_i = 0; $_i < $_len; ++$_i) {args[$_i + 1] = arguments[$_i];}
var bound = this._isBound() ? this._boundTo : null;
return this._then(function(obj) {
args[0] = obj;
return fn.apply(bound, args);
},
void 0,
void 0,
void 0,
void 0,
this[method]
);
};
};
Promise.extend('hello', function (res, bar) {
console.log('resolved value: ', res);
console.log('passed argument: ', bar);
return this.hello();
});
function Foo() {
this.deferred = Promise.defer();
}
Foo.prototype.hello = function (res) {
console.log('called method on bound object:', res);
return this.deferred.promise;
};
Foo.prototype.resolve = function (val) {
this.deferred.resolve(val);
};
var newFoo = new Foo();
Promise.bind(newFoo).return('hi').hello('bar').then(function (res) {
this.hello(res);
});
newFoo.resolve('keke');
Promise.resolve('hi').hello('bar'); // throws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment