Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # throat
  2. Throttle the parallelism of an asynchronous, promise returning, function / functions. This has special utility when you set the concurrency to `1`. That way you get a mutually exclusive lock.
  3. [![Build Status](https://img.shields.io/travis/ForbesLindesay/throat/master.svg)](https://travis-ci.org/ForbesLindesay/throat)
  4. [![Coverage Status](https://img.shields.io/coveralls/ForbesLindesay/throat/master.svg?style=flat)](https://coveralls.io/r/ForbesLindesay/throat?branch=master)
  5. [![Dependency Status](https://img.shields.io/david/ForbesLindesay/throat.svg)](https://david-dm.org/ForbesLindesay/throat)
  6. [![NPM version](https://img.shields.io/npm/v/throat.svg)](https://www.npmjs.com/package/throat)
  7. [![Greenkeeper badge](https://badges.greenkeeper.io/ForbesLindesay/throat.svg)](https://greenkeeper.io/)
  8. [![Sauce Test Status](https://saucelabs.com/browser-matrix/throat.svg)](https://saucelabs.com/u/throat)
  9. ## Installation
  10. npm install throat
  11. ## API
  12. ### throat(concurrency)
  13. This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).
  14. Example, only 2 of the following functions will execute at any one time:
  15. ```js
  16. const throat = require('throat')(2);
  17. // alternatively provide your own promise implementation
  18. const throat = require('throat')(require('promise'))(2);
  19. const promise = Promise.resolve();
  20. const resA = throat(() => /* async stuff... */ promise);
  21. const resB = throat(() => /* async stuff... */ promise);
  22. const resC = throat(() => /* async stuff... */ promise);
  23. const resD = throat(() => /* async stuff... */ promise);
  24. const resE = throat(() => /* async stuff... */ promise);
  25. ```
  26. ### throat(concurrency, worker)
  27. This returns a function that is an exact copy of `worker` except that it will only execute up to `concurrency` times in parallel before further requests are queued:
  28. ```js
  29. const throat = require('throat');
  30. // alternatively provide your own promise implementation
  31. const throat = require('throat')(require('promise'));
  32. const input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt'];
  33. const data = Promise.all(input.map(throat(2, fileName => readFile(fileName))));
  34. ```
  35. Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.
  36. ## License
  37. MIT