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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # release-zalgo
  2. Helps you write code with promise-like chains that can run both synchronously
  3. and asynchronously.
  4. ## Installation
  5. ```console
  6. $ npm install --save release-zalgo
  7. ```
  8. ## Usage
  9. If you use this module, you'll release **Ẕ̶̨̫̹̌͊͌͑͊̕͢͟a̡̜̦̝͓͇͗̉̆̂͋̏͗̍ͅl̡̛̝͍̅͆̎̊̇̕͜͢ģ̧̧͍͓̜̲͖̹̂͋̆̃̑͗̋͌̊̏ͅǫ̷̧͓̣͚̞̣̋̂̑̊̂̀̿̀̚͟͠ͅ**. You mustn't do that.
  10. Before you proceed, please read this great post by [Isaac
  11. Schlueter](http://izs.me/) on [Designing APIs for
  12. Asynchrony](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony).
  13. The first rule of using this package is to keep your external API consistent.
  14. The second rule is to accept the burden of controlling Ẕ̶̨̫̹̌͊͌͑͊̕͢͟a̡̜̦̝͓͇͗̉̆̂͋̏͗̍ͅl̡̛̝͍̅͆̎̊̇̕͜͢ģ̧̧͍͓̜̲͖̹̂͋̆̃̑͗̋͌̊̏ͅǫ̷̧͓̣͚̞̣̋̂̑̊̂̀̿̀̚͟͠ͅ by ensuring he does not escape your API boundary.
  15. With that out of the way… this package lets you write code that can run both
  16. synchronously and asynchronously. This is useful if you have fairly complex
  17. logic for which you don't want to write multiple implementations. See
  18. [`package-hash`](https://github.com/novemberborn/package-hash) for instance.
  19. This is best shown by example. Let's say you have a `hashFile()` function:
  20. ```js
  21. const crypto = require('crypto')
  22. const fs = require('fs')
  23. function hashFile (file) {
  24. return new Promise((resolve, reject) => {
  25. fs.readFile(file, (err, buffer) => err ? reject(err) : resolve(buffer))
  26. })
  27. .then(buffer => {
  28. const hash = crypto.createHash('sha1')
  29. hash.update(buffer)
  30. return hash.digest('hex')
  31. })
  32. }
  33. ```
  34. A synchronous version could be implemented like this:
  35. ```js
  36. function hashFileSync (file) {
  37. const buffer = fs.readFileSync(file)
  38. const hash = crypto.createHash('sha1')
  39. hash.update(buffer)
  40. return hash.digest('hex')
  41. }
  42. ```
  43. Here's the version that uses `release-zalgo`:
  44. ```js
  45. const crypto = require('crypto')
  46. const fs = require('fs')
  47. const releaseZalgo = require('release-zalgo')
  48. const readFile = {
  49. async (file) {
  50. return new Promise((resolve, reject) => {
  51. fs.readFile(file, (err, buffer) => err ? reject(err) : resolve(buffer))
  52. })
  53. },
  54. sync (file) {
  55. return fs.readFileSync(file)
  56. }
  57. }
  58. function run (zalgo, file) {
  59. return zalgo.run(readFile, file)
  60. .then(buffer => {
  61. const hash = crypto.createHash('sha1')
  62. hash.update(buffer)
  63. return hash.digest('hex')
  64. })
  65. }
  66. function hashFile (file) {
  67. return run(releaseZalgo.async(), file)
  68. }
  69. function hashFileSync (file) {
  70. const result = run(releaseZalgo.sync(), file)
  71. return releaseZalgo.unwrapSync(result)
  72. }
  73. ```
  74. Note how close the `run()` implementation is to the original `hashFile()`.
  75. Just don't do this:
  76. ```js
  77. function badExample (zalgo, file) {
  78. let buffer
  79. zalgo.run(readFile, file)
  80. .then(result => { buffer = result })
  81. const hash = crypto.createHash('sha1')
  82. hash.update(buffer)
  83. return hash.digest('hex')
  84. }
  85. ```
  86. This won't work asynchronously. Just pretend you're working with promises and
  87. you'll be OK.
  88. ## API
  89. First require the package:
  90. ```js
  91. const releaseZalgo = require('release-zalgo')
  92. ```
  93. ### `releaseZalgo.sync()`
  94. Returns a `zalgo` object that runs code synchronously:
  95. ```js
  96. const zalgo = releaseZalgo.sync()
  97. ```
  98. ### `releaseZalgo.async()`
  99. Returns a `zalgo` object that runs code asynchronously:
  100. ```js
  101. const zalgo = releaseZalgo.async()
  102. ```
  103. ### `releaseZalgo.unwrapSync(thenable)`
  104. Synchronously unwraps a [thenable], which is returned when running
  105. synchronously. Returns the [thenable]s fulfilment value, or throws its
  106. rejection reason. Throws if the [thenable] is asynchronous.
  107. ### `zalgo.run(executors, ...args)`
  108. When running synchronously, `executors.sync()` is called. When running
  109. asynchronously `executors.async()` is used. The executer is invoked immediately
  110. and passed the remaining arguments.
  111. For asynchronous execution a `Promise` is returned. It is fulfilled with
  112. `executors.async()`'s return value, or rejected if `executors.async()` throws.
  113. For synchronous execution a *[thenable]* is returned. It has the same methods as
  114. `Promise` except that callbacks are invoked immediately. The [thenable] is
  115. fulfilled with `executors.sync()`'s return value, or rejected if
  116. `executors.sync()` throws.
  117. ### `zalgo.all(arr)`
  118. When running synchronously, returns a new [thenable] which is fulfilled with
  119. an array, after unwrapping all items in `arr`.
  120. When running asynchronously, delegates to `Promise.all(arr)`.
  121. ### `zalgo.returns(value)`
  122. When running synchronously, returns a new [thenable] which is fulfilled with
  123. `value`.
  124. When running asynchronously, delegates to `Promise.resolve(value)`.
  125. ### `zalgo.throws(reason)`
  126. When running synchronously, returns a new [thenable] which is rejected with
  127. `reason`.
  128. When running asynchronously, delegates to `Promise.reject(reason)`.
  129. ### Thenables
  130. Thenables are returned when running sychronously. They're much like `Promise`s,
  131. in that they have `then()` and `catch()` methods. You can pass callbacks and
  132. they'll be invoked with the fulfilment value or rejection reason. Callbacks
  133. can return other thenables or throw exceptions.
  134. Note that `then()` and `catch()` must be called on the thenable, e.g.
  135. `thenable.then()`, not `(thenable.then)()`.
  136. Thenables should not be exposed outside of your API. Use
  137. `releaseZalgo.unwrapSync()` to unwrap them.
  138. [thenable]: #thenables