Skip to content

Instantly share code, notes, and snippets.

@rubeniskov
Last active November 28, 2015 22:12
Show Gist options
  • Save rubeniskov/8f28598b402d15f7f53d to your computer and use it in GitHub Desktop.
Save rubeniskov/8f28598b402d15f7f53d to your computer and use it in GitHub Desktop.
promise_decorator.js
function decorator(context, callbacks){
return (function(context, callbacks, $finally) {
function execute(callbacks, context, args, value) {
return callbacks.length ?
execute(callbacks, context, args, callbacks.pop().apply(context, args, value)) || value:value;
};
return {
context: context,
invoke: function(fn, args, context){
var value;
$finally.apply(this, [(value=execute([].concat(callbacks), context||this.context||this, args,
fn.apply(context||this, args))), args]);
return value;
},
then: function(callback){
typeof(callback)==='function' && callbacks.unshift(callback);
return this;
},
finally: function(onFinally){
$finally=onFinally;
return this;
}
};
})(context||this, callbacks||[], function(){})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment