• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Javascript] Jam crappy promise support forcefully into every last god damn function

Joined
May 23, 2008
Messages
1,071
Reaction score
574
Because duck it.

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
 */
 
Custom Title Activated
Loyal Member
Joined
May 23, 2011
Messages
1,607
Reaction score
589
TimeBomb - [Javascript] Jam crappy promise support forcefully into every last god damn function - RaGEZONE Forums
 
Back
Top