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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # p-cancelable [![Build Status](https://travis-ci.org/sindresorhus/p-cancelable.svg?branch=master)](https://travis-ci.org/sindresorhus/p-cancelable)
  2. > Create a promise that can be canceled
  3. Useful for animation, loading resources, long-running async computations, async iteration, etc.
  4. ## Install
  5. ```
  6. $ npm install p-cancelable
  7. ```
  8. ## Usage
  9. ```js
  10. const PCancelable = require('p-cancelable');
  11. const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
  12. const worker = new SomeLongRunningOperation();
  13. onCancel(() => {
  14. worker.close();
  15. });
  16. worker.on('finish', resolve);
  17. worker.on('error', reject);
  18. });
  19. (async () => {
  20. try {
  21. console.log('Operation finished successfully:', await cancelablePromise);
  22. } catch (error) {
  23. if (cancelablePromise.isCanceled) {
  24. // Handle the cancelation here
  25. console.log('Operation was canceled');
  26. return;
  27. }
  28. throw error;
  29. }
  30. })();
  31. // Cancel the operation after 10 seconds
  32. setTimeout(() => {
  33. cancelablePromise.cancel('Unicorn has changed its color');
  34. }, 10000);
  35. ```
  36. ## API
  37. ### new PCancelable(executor)
  38. Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`.<br>
  39. Cancelling will reject the promise with `PCancelable.CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
  40. ```js
  41. const PCancelable = require('p-cancelable');
  42. const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
  43. const job = new Job();
  44. onCancel.shouldReject = false;
  45. onCancel(() => {
  46. job.stop();
  47. });
  48. job.on('finish', resolve);
  49. });
  50. cancelablePromise.cancel(); // Doesn't throw an error
  51. ```
  52. `PCancelable` is a subclass of `Promise`.
  53. #### onCanceled(fn)
  54. Type: `Function`
  55. Accepts a function that is called when the promise is canceled.
  56. You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.
  57. ### PCancelable#cancel([reason])
  58. Type: `Function`
  59. Cancel the promise and optionally provide a reason.
  60. The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
  61. ### PCancelable#isCanceled
  62. Type: `boolean`
  63. Whether the promise is canceled.
  64. ### PCancelable.CancelError
  65. Type: `Error`
  66. Rejection reason when `.cancel()` is called.
  67. It includes a `.isCanceled` property for convenience.
  68. ### PCancelable.fn(fn)
  69. Convenience method to make your promise-returning or async function cancelable.
  70. The function you specify will have `onCancel` appended to its parameters.
  71. ```js
  72. const PCancelable = require('p-cancelable');
  73. const fn = PCancelable.fn((input, onCancel) => {
  74. const job = new Job();
  75. onCancel(() => {
  76. job.cleanup();
  77. });
  78. return job.start(); //=> Promise
  79. });
  80. const cancelablePromise = fn('input'); //=> PCancelable
  81. // …
  82. cancelablePromise.cancel();
  83. ```
  84. ## FAQ
  85. ### Cancelable vs. Cancellable
  86. [In American English, the verb cancel is usually inflected canceled and canceling—with one l.](http://grammarist.com/spelling/cancel/)<br>Both a [browser API](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable) and the [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises) use this spelling.
  87. ### What about the official [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises)?
  88. ~~It's still an early draft and I don't really like its current direction. It complicates everything and will require deep changes in the ecosystem to adapt to it. And the way you have to use cancel tokens is verbose and convoluted. I much prefer the more pragmatic and less invasive approach in this module.~~ The proposal was withdrawn.
  89. ## Related
  90. - [p-progress](https://github.com/sindresorhus/p-progress) - Create a promise that reports progress
  91. - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called
  92. - [More…](https://github.com/sindresorhus/promise-fun)
  93. ## License
  94. MIT © [Sindre Sorhus](https://sindresorhus.com)