Skip to content

Instantly share code, notes, and snippets.

@Morgul
Last active August 29, 2015 14:11
Show Gist options
  • Save Morgul/cfd937e66fa0978ec42a to your computer and use it in GitHub Desktop.
Save Morgul/cfd937e66fa0978ec42a to your computer and use it in GitHub Desktop.
A promise wrapper for AngularJS.
// ---------------------------------------------------------------------------------------------------------------------
// 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