diff options
Diffstat (limited to 'src/scenario/Future.js')
| -rw-r--r-- | src/scenario/Future.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/scenario/Future.js b/src/scenario/Future.js index 60fad9c5..30c2d902 100644 --- a/src/scenario/Future.js +++ b/src/scenario/Future.js @@ -6,6 +6,7 @@ angular.scenario.Future = function(name, behavior) { this.behavior = behavior; this.fulfilled = false; this.value = undefined; + this.parser = angular.identity; }; /** @@ -16,7 +17,38 @@ angular.scenario.Future = function(name, behavior) { angular.scenario.Future.prototype.execute = function(doneFn) { this.behavior(angular.bind(this, function(error, result) { this.fulfilled = true; + if (result) { + try { + result = this.parser(result); + } catch(e) { + error = e; + } + } this.value = error || result; doneFn(error, result); })); }; + +/** + * Configures the future to convert it's final with a function fn(value) + */ +angular.scenario.Future.prototype.parsedWith = function(fn) { + this.parser = fn; + return this; +}; + +/** + * Configures the future to parse it's final value from JSON + * into objects. + */ +angular.scenario.Future.prototype.fromJson = function() { + return this.parsedWith(angular.fromJson); +}; + +/** + * Configures the future to convert it's final value from objects + * into JSON. + */ +angular.scenario.Future.prototype.toJson = function() { + return this.parsedWith(angular.toJson); +}; |
