Skip to content

Instantly share code, notes, and snippets.

@deniswolf
Last active December 15, 2015 16:19
Show Gist options
  • Save deniswolf/5288647 to your computer and use it in GitHub Desktop.
Save deniswolf/5288647 to your computer and use it in GitHub Desktop.
build on idea of promises POC of framework full of chained callbacks
// kommunism.js
// build on false promises
// to chain callbacks
function PrimitiveKommunism(computationFunction, thisArg){
// todo: return promise that will resolve when computation is done
var someInnerComputationsResult = computationFunction.apply(thisArg, _.toArray(arguments));
function Kommunism(currentCallback, optionalCallbackParameters){
var currentArguments = _.toArray(arguments).slice(1);
var argumentsToPass = [someInnerComputationsResult].concat(currentArguments);
currentCallback.apply(this,argumentsToPass);
return Kommunism;
}
return Kommunism;
}
function callBack(){
console.log('callback',arguments);
}
socialism = new PrimitiveKommunism(function(){return 'Wait for the future, Komrade!'})
socialism(callBack,'z','x')(callBack)(callBack,1)(callBack,2)(callBack,3);
@deniswolf
Copy link
Author

that was the simple part,
now there is a next step ahead - how to make this system work with the promises across the whole flow
in a way that:
when I do some computations, they could end in a distant future
but all the callbacks will still receive them as promises or callback will be run on promises' resolves

@deniswolf
Copy link
Author

and on line 13:
I would like to know which context it is better to pass :/

@deniswolf
Copy link
Author

@jsmaker
Copy link

jsmaker commented Apr 2, 2013

why do you need _

http://jsfiddle.net/3k2TD/9/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment