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.

wrap.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict'
  2. var slice = [].slice
  3. module.exports = wrap
  4. // Wrap `fn`.
  5. // Can be sync or async; return a promise, receive a completion handler, return
  6. // new values and errors.
  7. function wrap(fn, callback) {
  8. var invoked
  9. return wrapped
  10. function wrapped() {
  11. var params = slice.call(arguments, 0)
  12. var callback = fn.length > params.length
  13. var result
  14. if (callback) {
  15. params.push(done)
  16. }
  17. try {
  18. result = fn.apply(null, params)
  19. } catch (error) {
  20. // Well, this is quite the pickle.
  21. // `fn` received a callback and invoked it (thus continuing the pipeline),
  22. // but later also threw an error.
  23. // We’re not about to restart the pipeline again, so the only thing left
  24. // to do is to throw the thing instead.
  25. if (callback && invoked) {
  26. throw error
  27. }
  28. return done(error)
  29. }
  30. if (!callback) {
  31. if (result && typeof result.then === 'function') {
  32. result.then(then, done)
  33. } else if (result instanceof Error) {
  34. done(result)
  35. } else {
  36. then(result)
  37. }
  38. }
  39. }
  40. // Invoke `next`, only once.
  41. function done() {
  42. if (!invoked) {
  43. invoked = true
  44. callback.apply(null, arguments)
  45. }
  46. }
  47. // Invoke `done` with one value.
  48. // Tracks if an error is passed, too.
  49. function then(value) {
  50. done(null, value)
  51. }
  52. }