Last active
August 29, 2015 14:11
-
-
Save Morgul/cfd937e66fa0978ec42a to your computer and use it in GitHub Desktop.
A promise wrapper for AngularJS.
This file contains 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
// --------------------------------------------------------------------------------------------------------------------- | |
// PromiseProxy - A proxy object that wraps a promise, and exposes `pending`, `result` and `error` properties. This is | |
// most useful when setting results of promises on the scope directly, so you can work with them in your templates. | |
// | |
// @module promise-proxy.js | |
// --------------------------------------------------------------------------------------------------------------------- | |
function PromiseProxyFactory(promise) | |
{ | |
var proxy = { | |
pending: true, | |
promise: promise | |
}; | |
promise.then( | |
function(result) | |
{ | |
proxy.result = result; | |
}, | |
function(error) | |
{ | |
proxy.error = error; | |
}); | |
return proxy; | |
} // end PromiseProxyFactory | |
// --------------------------------------------------------------------------------------------------------------------- | |
angular.module('promise-proxy', []).value('promise-proxy', PromiseProxyFactory); | |
// --------------------------------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment