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 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-q-limit

v1.0.3

Published

Adds $q.allLimit() method to Angular which allows $q.all() batching

Downloads

2,054

Readme

angular-q-limit

Adds $q.allLimit() method to Angular which allows $q.all() batching.

One of my biggest gripes with the Angular $q implementation is if you have a lot of Promises to resolve these can quickly overwhelm the Browser / Server.

This module adds the $q.allLimit() function which provides the same functionality as the regular $q.all() function but only allows a limited number of Promises to run at once.

NOTES:

  • Unless otherwise specified the default limit for .allLimit() calls is 1.
  • The limit number can be anywhere within the list of promises. e.g. $q.allLimit(3, promises...) or $q.allLimit(promises..., 3)
  • This module will also fire a progress notification if you need to monitor how many promises have completed

Install

  1. Add a reference to the script somewhere in your HTML:
<script src="/vendors/angular-q-limit/angular-q-limit.js"></script>
  1. Then add the module to your Angular header file:
var app = angular.module('app', [
	'angular-q-limit',
]);
  1. You can now use the limiter in code as part of the regular $q library:
app.controller(function($scope, $q) {

	// Run lots of things but only 3 at a time
	$q.allLimit(3, [
		// Lots of promises here ///
	])
		.then(function(data) {
			// Done!
		}, function(err) {
			// One promise died!
		}, function(progress) {
			// Progress updates! (progress will equal {completed: Number, count: Number, limit: Number})
		});

});

Examples

Each of the following examples assumes SomeModel is a Promise based structure like $http or ngResource. Each of these would perform some time consuming task like transmitting large quantities of data to or from the server.

Defined promises

This example runs 3 defined promise limited to 1 item of concurrency. The example shown only lists three promises but this can be extended indefinitely with the guarantee that only 2 items can run at once.

// Load different models and set $scope.data{1,2,3} to the return value
// Only allow two Promises to run at once

$q.limitAll(2, [

	SomeModel1.query().$promise
		.then(data => $scope.data1 = data),

	SomeModel2.query().$promise
		.then(data => $scope.data2 = data),

	SomeModel3.query().$promise
		.then(data => $scope.data3 = data),

	// More promises here ...

]).then(function() { // All done // }});

Dynamic promise creation

This example uses a dynamic array of items, creating a promise for each and finally executing them via $q.limitAll() with a maximum of 3 items running concurrently.

var stuff = [ // Very big array of IDs to request // ];

$q.allLimit(3,
	stuff.map(function(item) {
		return SomeModel.get({id: item}).$promise;
	}),
});