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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # gensync
  2. This module allows for developers to write common code that can share
  3. implementation details, hiding whether an underlying request happens
  4. synchronously or asynchronously. This is in contrast with many current Node
  5. APIs which explicitly implement the same API twice, once with calls to
  6. synchronous functions, and once with asynchronous functions.
  7. Take for example `fs.readFile` and `fs.readFileSync`, if you're writing an API
  8. that loads a file and then performs a synchronous operation on the data, it
  9. can be frustrating to maintain two parallel functions.
  10. ## Example
  11. ```js
  12. const fs = require("fs");
  13. const gensync = require("gensync");
  14. const readFile = gensync({
  15. sync: fs.readFileSync,
  16. errback: fs.readFile,
  17. });
  18. const myOperation = gensync(function* (filename) {
  19. const code = yield* readFile(filename, "utf8");
  20. return "// some custom prefix\n" + code;
  21. });
  22. // Load and add the prefix synchronously:
  23. const result = myOperation.sync("./some-file.js");
  24. // Load and add the prefix asynchronously with promises:
  25. myOperation.async("./some-file.js").then(result => {
  26. });
  27. // Load and add the prefix asynchronously with promises:
  28. myOperation.errback("./some-file.js", (err, result) => {
  29. });
  30. ```
  31. This could even be exposed as your official API by doing
  32. ```js
  33. // Using the common 'Sync' suffix for sync functions, and 'Async' suffix for
  34. // promise-returning versions.
  35. exports.myOperationSync = myOperation.sync;
  36. exports.myOperationAsync = myOperation.async;
  37. exports.myOperation = myOperation.errback;
  38. ```
  39. or potentially expose one of the async versions as the default, with a
  40. `.sync` property on the function to expose the synchronous version.
  41. ```js
  42. module.exports = myOperation.errback;
  43. module.exports.sync = myOperation.sync;
  44. ````
  45. ## API
  46. ### gensync(generatorFnOrOptions)
  47. Returns a function that can be "await"-ed in another `gensync` generator
  48. function, or executed via
  49. * `.sync(...args)` - Returns the computed value, or throws.
  50. * `.async(...args)` - Returns a promise for the computed value.
  51. * `.errback(...args, (err, result) => {})` - Calls the callback with the computed value, or error.
  52. #### Passed a generator
  53. Wraps the generator to populate the `.sync`/`.async`/`.errback` helpers above to
  54. allow for evaluation of the generator for the final value.
  55. ##### Example
  56. ```js
  57. const readFile = function* () {
  58. return 42;
  59. };
  60. const readFileAndMore = gensync(function* (){
  61. const val = yield* readFile();
  62. return 42 + val;
  63. });
  64. // In general cases
  65. const code = readFileAndMore.sync("./file.js", "utf8");
  66. readFileAndMore.async("./file.js", "utf8").then(code => {})
  67. readFileAndMore.errback("./file.js", "utf8", (err, code) => {});
  68. // In a generator being called indirectly with .sync/.async/.errback
  69. const code = yield* readFileAndMore("./file.js", "utf8");
  70. ```
  71. #### Passed an options object
  72. * `opts.sync`
  73. Example: `(...args) => 4`
  74. A function that will be called when `.sync()` is called on the `gensync()`
  75. result, or when the result is passed to `yield*` in another generator that
  76. is being run synchronously.
  77. Also called for `.async()` calls if no async handlers are provided.
  78. * `opts.async`
  79. Example: `async (...args) => 4`
  80. A function that will be called when `.async()` or `.errback()` is called on
  81. the `gensync()` result, or when the result is passed to `yield*` in another
  82. generator that is being run asynchronously.
  83. * `opts.errback`
  84. Example: `(...args, cb) => cb(null, 4)`
  85. A function that will be called when `.async()` or `.errback()` is called on
  86. the `gensync()` result, or when the result is passed to `yield*` in another
  87. generator that is being run asynchronously.
  88. This option allows for simpler compatibility with many existing Node APIs,
  89. and also avoids introducing the extra even loop turns that promises introduce
  90. to access the result value.
  91. * `opts.name`
  92. Example: `"readFile"`
  93. A string name to apply to the returned function. If no value is provided,
  94. the name of `errback`/`async`/`sync` functions will be used, with any
  95. `Sync` or `Async` suffix stripped off. If the callback is simply named
  96. with ES6 inference (same name as the options property), the name is ignored.
  97. * `opts.arity`
  98. Example: `4`
  99. A number for the length to set on the returned function. If no value
  100. is provided, the length will be carried over from the `sync` function's
  101. `length` value.
  102. ##### Example
  103. ```js
  104. const readFile = gensync({
  105. sync: fs.readFileSync,
  106. errback: fs.readFile,
  107. });
  108. const code = readFile.sync("./file.js", "utf8");
  109. readFile.async("./file.js", "utf8").then(code => {})
  110. readFile.errback("./file.js", "utf8", (err, code) => {});
  111. ```
  112. ### gensync.all(iterable)
  113. `Promise.all`-like combinator that works with an iterable of generator objects
  114. that could be passed to `yield*` within a gensync generator.
  115. #### Example
  116. ```js
  117. const loadFiles = gensync(function* () {
  118. return yield* gensync.all([
  119. readFile("./one.js"),
  120. readFile("./two.js"),
  121. readFile("./three.js"),
  122. ]);
  123. });
  124. ```
  125. ### gensync.race(iterable)
  126. `Promise.race`-like combinator that works with an iterable of generator objects
  127. that could be passed to `yield*` within a gensync generator.
  128. #### Example
  129. ```js
  130. const loadFiles = gensync(function* () {
  131. return yield* gensync.race([
  132. readFile("./one.js"),
  133. readFile("./two.js"),
  134. readFile("./three.js"),
  135. ]);
  136. });
  137. ```