Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # p-throttle
  2. > [Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions
  3. It also works with normal functions.
  4. Useful for rate limiting calls to an external API, for example.
  5. ## Install
  6. ```
  7. $ npm install p-throttle
  8. ```
  9. ## Usage
  10. Here, the throttled function is only called twice a second:
  11. ```js
  12. const pThrottle = require('p-throttle');
  13. const now = Date.now();
  14. const throttle = pThrottle({
  15. limit: 2,
  16. interval: 1000
  17. });
  18. const throttled = throttle(index => {
  19. const secDiff = ((Date.now() - now) / 1000).toFixed();
  20. return Promise.resolve(`${index}: ${secDiff}s`);
  21. });
  22. for (let i = 1; i <= 6; i++) {
  23. throttled(i).then(console.log);
  24. }
  25. //=> 1: 0s
  26. //=> 2: 0s
  27. //=> 3: 1s
  28. //=> 4: 1s
  29. //=> 5: 2s
  30. //=> 6: 2s
  31. ```
  32. ## API
  33. ### pThrottle(options)
  34. Returns a `throttle` function.
  35. Returns a throttled version of `fn`.
  36. #### options
  37. Type: `object`
  38. Both the `limit` and `interval` options must be specified.
  39. ##### limit
  40. Type: `number`
  41. Maximum number of calls within an `interval`.
  42. ##### interval
  43. Type: `number`
  44. Timespan for `limit` in milliseconds.
  45. #### strict
  46. Type: `boolean`\
  47. Default: `false`
  48. Use a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval.
  49. ### throttle(function_)
  50. #### function_
  51. Type: `Function`
  52. Promise-returning/async function or a normal function.
  53. ### throttledFn.abort()
  54. Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
  55. ### throttledFn.isEnabled
  56. Type: `boolean`\
  57. Default: `true`
  58. Whether future function calls should be throttled and count towards throttling thresholds.
  59. ## Related
  60. - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
  61. - [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
  62. - [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
  63. - [More…](https://github.com/sindresorhus/promise-fun)