npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

promise-unwrap

v0.0.2

Published

Angular: Unwrap promises and use them directly on the view, no controller necessary

Readme

promise-unwrap

Unwrap promises directly on the view, no controller necessary.

What is promise-unwrap?

promise-unwrap is an Angular module that exposes filters that let you unwrap promises in your view, without the boilerplate of

// in my-controller.js
var vm = this;
myService.asyncOp().then(function(results) {
  vm.results = results;
});

Instead, attach someService directly to the controller:

// in my-controller.js
var vm = this;
vm.myService = myService;

And in the view:

{{ myCtrl.myService.asyncOp() | resolvePromise }}

Resolve promise will return undefined (which will not render) before the promise has resolved, and if it is caught.

How to use

  • Include promise-unwrap.min.js in your HTML file, or concatenate it to your project's javascript file, etc.
  • Import 'promise-unwrap' in your module.
angular.module('my-module', [
  // all your other dependencies...
  'promise-unwrap'
]);
  • Expose promises directly to the view, e.g.
// in my-controller.js
var vm = this;
vm.myService = myService;
  • And unwrap the promise directly in the view.
{{ myCtrl.myService.asyncOp() | resolvePromise }}

API

resolvePromise

Type: filter
Parameter: promise
Returns: undefined if the promise has not resolved or was rejected, and the resolved value if the promise has resolved.

catchPromise

Type: filter
Parameter: promise
Returns: undefined if the promise has not resolved or was resolved, and the rejected value if the promise was rejected.

promiseState

Type: filter
Parameter: promise
Returns: 'pending', 'resolved' or 'rejected'

PromiseUnwrap.PromiseStoreService

Type: service

CachedPromise

Type: constructor
Parameter: promise
Returns: CachedPromise

Each CachedPromise cp is an object with the following schema:

cp.promise // original promise
cp.resolved // resolved value, if available
cp.rejected // rejected value, if available
cp.promiseState // 'pending', 'resolved' or 'rejected'
cp.remove() // removes cp from the PromiseStore

getCachedPromise

Type: function
Parameter: promise
Returns: An existing CachedPromise if it exists in the store, otherwise, a new CachedPromise. This new CachedPromise is put in the store.

FAQ

What if I need to operate on the results of an asynchronous call?

Example:

{{ (myCtrl.myService.asyncOp() | resolvePromise ).data.members.count }}

Errors fail silently in Angular views, so nothing blows up before the promise resolves.

What if my function returns a new instance of a promise every time?

The filters will not work with functions that return new promises every time, as the library compares promises for equality. If used in the view, you will immediately get an infinite digests error.

Example:

// this will work
var asyncOpPromise;
function getAsyncThing() {
  if (!asyncOpPromise) {
    asyncOpPromise = someAsyncOperation();
  }
  return asyncOpPromise; // this promise is always the same reference
}

// this will not work
function getModifiedAsyncThing() {
  // .then returns a new promise every time... this will cause an infinite digest error
  return getAsyncThing().then(function(thing) {
    return thing + ' was modified';
  });
}

// Workaround 1: don't call "then" every time the function is called, but cache it.
var modifiedAsyncOpPromise;
function getModifiedAsyncThing() {
  if (!modifiedAsyncOpPromise) {
    modifiedAsyncOpPromise = getAsyncThing().then(function(thing) {
      return thing + ' was modified';
    });
  }
  return modifiedAsyncOpPromise;
}
<!-- Workaround 2: Do the modifications directly in the view, if they are simple -->
<span ng-if="(myService.getAsyncThing() | promiseState) === 'resolved'">
  {{ (myService.getAsyncThing() | resolvePromise) + ' was modified' }}
</span>

Run Tests

npm install
npm install -g gulp
gulp test

Other commands

gulp
gulp lint
gulp test
gulp watch
gulp build

Future

  • Remove dependency on lodash
  • Catch errors gracefully when the parameter to the filters is not a promise, or wrap the parameter in $q.when, etc.
  • Thank you!

Contact me

Robert Balicki, [email protected]