Created
August 18, 2016 15:15
-
-
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.
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
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