Last active
November 28, 2015 22:12
-
-
Save rubeniskov/8f28598b402d15f7f53d to your computer and use it in GitHub Desktop.
promise_decorator.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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