Coder Social home page Coder Social logo

tomeraberbach / limit-concur Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 0.0 447 KB

⚖️ Limit an async function's concurrency with ease!

Home Page: https://npm.im/limit-concur

License: Apache License 2.0

TypeScript 100.00%
concurrency npm-package nodejs javascript async promise rate-limiting npm-module

limit-concur's Introduction

limit-concur

Limit an async function's concurrency with ease!

Features

  • Tiny: only ~320 B minzipped
  • Flexible: works for any function that returns a Promise

Install

$ npm i limit-concur

Usage

import got from 'got'
import limitConcur from 'limit-concur'

const categories = await got(
  `https://api.chucknorris.io/jokes/categories`,
).json()

const getChuckNorrisJoke = async category => {
  const { value } = await got(`https://api.chucknorris.io/jokes/random`, {
    searchParams: {
      category,
    },
  }).json()
  return value
}

const limitedGetChuckNorrisJoke = limitConcur(4, getChuckNorrisJoke)

// At most 4 requests are pending at any given time!
const jokes = await Promise.all(categories.map(limitedGetChuckNorrisJoke))
console.log(jokes)
//=> [
//     'Chuck Norris once rode a nine foot grizzly bear through an automatic car wash, instead of taking a shower.',
//     "Chuck Norris is actually the front man for Apple. He let's Steve Jobs run the show when he's on a mission. Chuck Norris is always on a mission.",
//     "Bill Gates thinks he's Chuck Norris. Chuck Norris actually laughed. Once.",
//     'Chuck Norris can install iTunes without installing Quicktime.',
//     ...
//   ]

You can get an API equivalent to p-limit like so:

import limitConcur from 'limit-concur'

const limit = limitConcur(1, fn => fn())

const input = [
  limit(() => fetchSomething(`foo`)),
  limit(() => fetchSomething(`bar`)),
  limit(() => doSomething()),
]

const result = await Promise.all(input)
console.log(result)
//=> [ ..., ..., ... ]

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

For pull requests, please read the contributing guidelines.

License

Apache License 2.0

This is not an official Google product.

limit-concur's People

Contributors

tomeraberbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

limit-concur's Issues

fast-check tips to detect race conditions

👋 Hello,

I just opened the issue in case it can be helpful for the project.

fast-check comes with a built-in arbitrary to ease detection of race conditions. You might be interested into relying on it in your tests too. See:

Please feel free to close the issue. It was mostly to let you know about the feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.