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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # p-retry
  2. > Retry a promise-returning or async function
  3. It does exponential backoff and supports custom retry strategies for failed operations.
  4. ## Install
  5. ```
  6. $ npm install p-retry
  7. ```
  8. ## Usage
  9. ```js
  10. const pRetry = require('p-retry');
  11. const fetch = require('node-fetch');
  12. const run = async () => {
  13. const response = await fetch('https://sindresorhus.com/unicorn');
  14. // Abort retrying if the resource doesn't exist
  15. if (response.status === 404) {
  16. throw new pRetry.AbortError(response.statusText);
  17. }
  18. return response.blob();
  19. };
  20. (async () => {
  21. console.log(await pRetry(run, {retries: 5}));
  22. })();
  23. ```
  24. ## API
  25. ### pRetry(input, options?)
  26. Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the maximum number of retries is reached. It then rejects with the last rejection reason.
  27. Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)
  28. #### input
  29. Type: `Function`
  30. Receives the current attempt number as the first argument and is expected to return a `Promise` or any value.
  31. #### options
  32. Type: `object`
  33. Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module.
  34. ##### onFailedAttempt(error)
  35. Type: `Function`
  36. Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively.
  37. ```js
  38. const run = async () => {
  39. const response = await fetch('https://sindresorhus.com/unicorn');
  40. if (!response.ok) {
  41. throw new Error(response.statusText);
  42. }
  43. return response.json();
  44. };
  45. (async () => {
  46. const result = await pRetry(run, {
  47. onFailedAttempt: error => {
  48. console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`);
  49. // 1st request => Attempt 1 failed. There are 4 retries left.
  50. // 2nd request => Attempt 2 failed. There are 3 retries left.
  51. // …
  52. },
  53. retries: 5
  54. });
  55. console.log(result);
  56. })();
  57. ```
  58. The `onFailedAttempt` function can return a promise. For example, you can do some async logging:
  59. ```js
  60. const pRetry = require('p-retry');
  61. const logger = require('./some-logger');
  62. const run = async () => { … };
  63. (async () => {
  64. const result = await pRetry(run, {
  65. onFailedAttempt: async error => {
  66. await logger.log(error);
  67. }
  68. });
  69. })();
  70. ```
  71. If the `onFailedAttempt` function throws, all retries will be aborted and the original promise will reject with the thrown error.
  72. ### pRetry.AbortError(message)
  73. ### pRetry.AbortError(error)
  74. Abort retrying and reject the promise.
  75. ### message
  76. Type: `string`
  77. Error message.
  78. ### error
  79. Type: `Error`
  80. Custom error.
  81. ## Tip
  82. You can pass arguments to the function being retried by wrapping it in an inline arrow function:
  83. ```js
  84. const pRetry = require('p-retry');
  85. const run = async emoji => {
  86. // …
  87. };
  88. (async () => {
  89. // Without arguments
  90. await pRetry(run, {retries: 5});
  91. // With arguments
  92. await pRetry(() => run('🦄'), {retries: 5});
  93. })();
  94. ```
  95. ## Related
  96. - [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time
  97. - [More…](https://github.com/sindresorhus/promise-fun)