Skip to content

Instantly share code, notes, and snippets.

@g-i-o-
Created August 18, 2016 15:15
Show Gist options
  • Save g-i-o-/54ab7b06f01ad03c8f0e40559b7a603e to your computer and use it in GitHub Desktop.
Save g-i-o-/54ab7b06f01ad03c8f0e40559b7a603e to your computer and use it in GitHub Desktop.
Serializing promise returning functions: because sometimes you need to process each call one at a time.
angular.module('service.serialize-promised-fn', [
])
/** serializePromisedFn function decorator.
* Decorates a promise-returning function so that
* concurrent calls occur in a serialized manner,
* that is, each call takes turn in a queue and gets
* processed one at a time, in call order.
*/
.service('serializePromisedFn', function($q){
return function serializePromisedFn(fn){
var series = $q.resolve();
return function serializedPromisedFn(){
var context = this;
var args = Array.prototype.slice.call(arguments);
series = series.then(function(){
return fn.apply(context, args);
});
return series;
};
};
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment