• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

limits calls to functions that return promises

promise-limit npm version npm Build Status

Limit outstanding calls to promise returning functions, or a semaphore for promises. You might want to do this to reduce load on external services, or reduce memory usage when processing large batches of jobs.

npm install promise-limit
var promiseLimit = require('promise-limit')

var limit = promiseLimit(2)

var jobs = ['a', 'b', 'c', 'd', 'e']

Promise.all(jobs.map((name) => {
  return limit(() => job(name))
})).then(results => {
  console.log()
  console.log('results:', results)
})

function job (name) {
  var text = `job ${name}`
  console.log('started', text)

  return new Promise(function (resolve) {
    setTimeout(() => {
      console.log('       ', text, 'finished')
      resolve(text)
    }, 100)
  })
}

will output:

started job a
started job b
        job a finished
        job b finished
started job c
started job d
        job c finished
        job d finished
started job e
        job e finished

results: [ 'job a', 'job b', 'job c', 'job d', 'job e' ]

API

var promiseLimit = require('promise-limit')

promiseLimit(concurrency?: Number) -> limit

Returns a function that can be used to wrap promise returning functions, limiting them to concurrency outstanding calls.

  • concurrency the concurrency, i.e. 1 will limit calls to one at a time, effectively in sequence or serial. 2 will allow two at a time, etc. 0 or undefined specify no limit, and all calls will be run in parallel.

limit

limit(fn: () -> Promise<T>) -> Promise<T>

A function that limits calls to fn, based on concurrency above. Returns a promise that resolves or rejects the same value or error as fn. All functions are executed in the same order in which they were passed to limit. fn must return a promise.

  • fn a function that is called with no arguments and returns a promise. You can pass arguments to your function by putting it inside another function, i.e. () -> myfunc(a, b, c).

limit.map

limit.map(items: [T], mapper: (T) -> Promise<R>) -> Promise<[R]>

Maps an array of items using mapper, but limiting the number of concurrent calls to mapper with the concurrency of limit. If at least one call to mapper returns a rejected promise, the result of map is a the same rejected promise, and no further calls to mapper are made.

limit.queue

limit.queue: Number

Returns the queue length, the number of jobs that are waiting to be started. You could use this to throttle incoming jobs, so the queue doesn't overwhealm the available memory - for e.g. pause() a stream.

We're Hiring!

Featurist provides full stack, feature driven development teams. Want to join us? Check out our career opportunities.

More Repositories

1

coypu

Intuitive, robust browser automation for .Net
C#
563
star
2

hyperdom

A fast, feature rich and simple framework for building dynamic browser applications.
TypeScript
162
star
3

sworm

a write-only ORM for Node.js
JavaScript
146
star
4

pogoscript

A readable, DSL friendly programming language with excellent concurrency primitives
JavaScript
127
star
5

interfaceable

Strict interfaces in Ruby
Ruby
92
star
6

browser-monkey

Reliable DOM testing
JavaScript
53
star
7

css-to-xpath

Convert CSS selectors to XPaths, in JavaScript
PogoScript
41
star
8

vdom-query

Traverses and manipulates virtual-dom trees
JavaScript
26
star
9

httpism

Resource-oriented HTTP client for node.js and browsers
JavaScript
25
star
10

xpath-builder

JavaScript DSL for building complex XPath expressions
PogoScript
25
star
11

lowscore

a very small underscore, for browser apps that like to watch their weight
JavaScript
24
star
12

node-ebnf-diagram

Generate png diagrams from Extended Backus–Naur Form (EBNF) grammars
JavaScript
20
star
13

jquery-sendkeys

JavaScript
20
star
14

strictly_fake

Stub that automatically verifies that stubbed methods exist and the signatures match the original.
Ruby
19
star
15

full-stack-js-testing-demo

javascript web app full stack browser testing demo
JavaScript
18
star
16

SelfishHttp

self hosting HTTP server for testing in .net
C#
18
star
17

codesandbox-example-links

Generates "run this example" links for your readme.md
JavaScript
15
star
18

html2js-browserify

for turning crummy HTML into beautiful Javascript, with browserify
JavaScript
15
star
19

coypu-jvm

Intuitive, robust browser automation
Java
12
star
20

karma-server-side

Run server-side code from karma tests
JavaScript
12
star
21

dslify

JavaScript function rewriter, for little DSLs
PogoScript
10
star
22

snowdock

a docker deployment utility, with special support for hot-swapping updates to websites
PogoScript
10
star
23

spawn-cmd

node.js module to run windows and unix processes the same way
PogoScript
8
star
24

Everest

Resource-oriented HTTP client for .net
C#
8
star
25

browser-pdf-support

detect PDF support in the browser
JavaScript
7
star
26

hyperdom-router

model driven router for hyperdom
JavaScript
7
star
27

fixture_farm

Why writing fixtures when they can write themselves?
Ruby
6
star
28

fs-tree

Directory hierarchy builder utility for node.js
PogoScript
5
star
29

trytryagain

Keep retrying until it works. Actually a very reliable testing strategy.
PogoScript
5
star
30

vinehill

In process bridge between http requests and connect based servers
JavaScript
5
star
31

banana-shark

A strictly synchronous test runner for JavaScript
JavaScript
4
star
32

browserify-server

browserifying JS server, with support for multiple bundles
JavaScript
4
star
33

bo-selector

JavaScript CSS selector parser
JavaScript
4
star
34

shellsubstitute

JavaScript
4
star
35

cooperative

Cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js.
JavaScript
4
star
36

oracle-client-buildpack

Shell
4
star
37

hyperdom-modal

Accessible modal built with Hyperdom that uses <dialog> element
JavaScript
3
star
38

rack-stubs

Rack middleware for stubbing responses from HTTP web services
Ruby
3
star
39

routism

Minimal, fast javascript URL router
PogoScript
3
star
40

create-hyperdom-app

Hyperdom starter kit
JavaScript
3
star
41

qo

finally, a task runner for insect people
PogoScript
3
star
42

pogo-examples

Side-by-side examples of pogoscript to javascript compilation
JavaScript
3
star
43

fireworks

PogoScript
3
star
44

htmlism

Pogoscript template engine
PogoScript
3
star
45

hyperdom-ace-editor

A hyperdom component that embeds the ace editor
JavaScript
3
star
46

flynn-pr-apps

Review apps on flynn
JavaScript
3
star
47

crc-pad

A browser-based, iPad-friendly CRC card editor
JavaScript
3
star
48

vim-pogoscript

PogoScript support for Vim
Vim Script
3
star
49

cappie

Instant capybara/cucumber/selenium configuration for non-ruby web apps
Ruby
3
star
50

stitchy

Command-line wrapper for stitch
PogoScript
3
star
51

rest-browser

An HTML client for REST APIs, designed to be embedded into the API itself
JavaScript
2
star
52

language

DSLs for the washed masses
JavaScript
2
star
53

build-status

A single BIG page showing users of Team City if they're an idiot or not.
PogoScript
2
star
54

mock-xhr-router

JavaScript
2
star
55

invertism

A dependency inversion utility for JavaScript
PogoScript
2
star
56

pogo-react

React.js components in PogoScript instead of JSX
PogoScript
2
star
57

hyperdom-semantic-ui

semantic-ui components for hyperdom
JavaScript
2
star
58

hyperdom-medium-editor

medium-editor for hyperdom
JavaScript
1
star
59

cloqwork

An experiment in programming experience
JavaScript
1
star
60

hyperdom-sortable

sortable lists for hyperdom
JavaScript
1
star
61

blocker

An HTTP proxy that blocks unwanted requests
Ruby
1
star
62

node-anticipate

retries asynchronous functions until they succeed
JavaScript
1
star
63

ringleader

Experimental distributed job queue using node.js and websockets
PogoScript
1
star
64

unset-timeout

resets any setTimeout or setInterval between mocha tests
JavaScript
1
star
65

hobostyle

Dynamic CSS for your web page!!!
JavaScript
1
star
66

node-featurist

CLI to create a new project with the Featurist stack
JavaScript
1
star
67

Bounce.MsDeploy

msdeploy tasks for bounce
C#
1
star
68

hyperdom-interactjs

multi-touch gestures for hyperdom using interact.js
JavaScript
1
star
69

generate-docker-certs

Generate client and server certificates just like `docker-machine regenerate-certs`
Go
1
star
70

waitforsocket

wait for a socket to open
PogoScript
1
star
71

GotFood

🍕 Gets food to hungry people
Ruby
1
star
72

dateism

JavaScript
1
star
73

tweetsonos

Ruby
1
star
74

wiblr

Cloud HTTP Debugging Proxy
JavaScript
1
star
75

html-pdf-server

URL -> PDF
JavaScript
1
star
76

thursby

PogoScript
1
star
77

hyperdom-draggabilly

A hyperdom component that makes elements draggable
HTML
1
star
78

fs

for .net, some simple filesystem operations: copy, move, delete and create.
C#
1
star
79

sworm-schema

JavaScript
1
star
80

hyperdom-loader

A magic asynchronous loader thingy for hyperdom
JavaScript
1
star
81

PogoScript.tmbundle

PogoScript support for TextMate
1
star
82

somepx

converse in px
JavaScript
1
star
83

modelism

A schema language for validating JavaScript objects
JavaScript
1
star
84

plastiq-throttle

JavaScript
1
star
85

angry-unicorn

Fix up the Github UI when they do something silly
JavaScript
1
star
86

extract-dates

Finds dates in strings
PogoScript
1
star
87

featurist-hubot

A robot
CoffeeScript
1
star
88

mocha-test-name

sets the current mocha test name to a global variable (because global variables are awesome)
JavaScript
1
star
89

nodeapp

just a simple node app that can be Dockerized
Shell
1
star
90

squashy

Packs HTML, inlining javascripts and stylesheets
JavaScript
1
star
91

procession

Starts a long-running process and blocks until it's ready to roll
Ruby
1
star
92

hyperdom-ckeditor

JavaScript
1
star
93

promise-builder

Builds up promises step by step
JavaScript
1
star
94

hyperdom-zeroclipboard

zeroclipboard for hyperdom
JavaScript
1
star
95

meddler

A programmable http proxy for node
PogoScript
1
star
96

cucumber-parallel

Runs cucumber scenarios in parallel
Ruby
1
star
97

cocos-pogo-template

A template for building cocos2d-js games in PogoScript
PogoScript
1
star
98

babel-preset-hyperdom

JavaScript
1
star
99

WebApi.ControllerTemplates

Templates for WebApi controllers
C#
1
star
100

finished-promise

Synchronous implementation of Promise for use in tests
JavaScript
1
star