Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[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
588
TimeBomb - [Javascript] Jam crappy promise support forcefully into every last god damn function - RaGEZONE Forums
 
Back
Top