- Joined
- May 23, 2008
- Messages
- 1,071
- Reaction score
- 574
Because duck it.
Example Usage:
PHP:
// NOTE TO HUMANITY: Don't use this code until humans have devolved to the point where we no longer have thumbs.
Object.defineProperty(Function.prototype, 'promise', {
get: function() {
var origFunc = this;
return function() {
var result = origFunc.apply(this, arguments);
return {
then: function then(callback) {
return callback.promise(result);
},
done: function done(callback) {
return callback(result);
}
};
};
}
});
Example Usage:
PHP:
// NOTE TO APE HUMANITY: Here is a banana. https://imgur.com/gallery/z4T2iMJ
var testFunc = function a(a, b, c) { return a+b+c; };
testFunc.promise = 'abc';
var testFuncPromise = testFunc.promise(1,1,2)
.then(function c(result) {
result += 1;
console.log(result);
return result;
})
.then(function c(result) {
result -= 1;
console.log(result);
return result;
})
.done(function c(result) {
result -= 1;
console.log(result);
return result;
})
;
console.log('testFuncPromise: ' + testFuncPromise);
/* Outputs:
* 5
* 4
* 3
* testFuncPromise: 3
*/